Question

What type should I use to it compile for s parameter in the constructor? I tried unsigned char s[32] and unsigned char *s but both give compiler error:

array initializer must be an initializer list or string literal

struct foo
{
    size_t a;
    size_t b;
    unsigned char c[32];

    constexpr foo(size_t a_, size_t b_, ??? s) : 
        a(a_), 
        b(b_), 
        c(s)
    { 
    }

}
Was it helpful?

Solution

The closest thing you could use is variadic template:

struct foo
{
    size_t a;
    size_t b;
    unsigned char c[32];

    template<typename ... UnsignedChar>
    constexpr foo(size_t a_, size_t b_, UnsignedChar ... s) : 
        a(a_), 
        b(b_), 
        c{s...} //use curly braces here
    { 
    }
};

And then use it as:

foo f(10, 20, 1,2,3,4,5, .....,32);
     //a,  b, -------s-----------

Note that if you pass less than 32 values for the third argument s, the rest of the elements of c will be initialized with 0, and if you pass more than 32 values, the compiler (I guess) will give warning (or error!).

Or use std::array<unsigned char, 32> instead of unsigned char[32].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top