Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit char array length in constructor

Using Extended Embedded Cpp. How can I make this result in a compilation error in a release build:

Param okParam("Yeah!"); // this line should be ok
Param nOkParam("REEEEEEEEEEE"); // too big array, not ok. compiler error.

where:

int const c_max = 10;

template<int N>
struct Param
{
  char value[c_max];

  Param(char const (&p_value)[N])
  {
     memcpy(value, p_value, sizeof(p_value));
  }
};

I don't think you can template the constructor, so the entire struct needs to be templated, right?


I want this to provide a clean compiler error so that the person using this will notice it immediately.

Our version of extended embedded C++ doesn't provide any stl containers, I'm not sure if it's even possible.

I'm looking for some way to make the template result in a good compilation error. Sadly I can't use boost either, since the platform would not support it.

like image 871
0xbaadf00d Avatar asked Mar 04 '23 23:03

0xbaadf00d


1 Answers

You have basically two solutions: SFINAE (C++98) or static_assert (C++11):

SFINAE

You can provide a constructor for Param only for char arrays less than a given size long. In C++98 this looks a bit ugly, but it works:

#include <cstddef>

template<bool b>
struct enable_if {};

template<>
struct enable_if<true>
{
    typedef int type;
};


template<std::size_t MAXSIZE>
struct Param
{
    template<std::size_t SIZE>
    explicit Param(
        char const (&input) [SIZE],
        std::size_t = sizeof(typename enable_if<SIZE < MAXSIZE>::type) // SFINAE at work
    ) { (void) input; }
};

int main()
{
    // "hello": char const[6], 6 < 7, OK
    Param<7> p1("hello");

    // "hello world": char const[12], 12 >= 7, KO
    Param<7> p2("hello world"); // ugly error here
}

Live demo

Assert (C++11 only)

Inside the constructor of Param, you can check if the supplied char array is too big and pop a readable error at compilation-time:

#include <cstddef>
#include <type_traits>

template<std::size_t MAXSIZE>
struct Param
{
    template<std::size_t SIZE>
    explicit Param(char const (&input) [SIZE])
    { static_assert(sizeof(input) < MAXSIZE, "input is too big."); }
};

int main()
{
    // "hello": char const[6], 6 < 7, OK
    Param<7> p1("hello");

    // "hello world": char const[12], 12 >= 7, KO
    Param<7> p2("hello world"); // "error: static assertion failed: input is too big."
}

Live demo

like image 53
YSC Avatar answered Mar 29 '23 03:03

YSC