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.
You have basically two solutions: SFINAE (C++98) or static_assert
(C++11):
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With