Wen Siang Lin
Wen Siang Lin

Reputation: 31

C++ static const array initializer

I have the following class:

const unsigned E = 256;

class A {
    public:
        static const unsigned x[E];
    ...
}

and I want to initialize x as follows:

const unsigned A::x[E] = { 1, 2, 3, ..., E };

The above assignment seems to be trivial for now. But, the point is to initialize the value of array x based on the index. A quick try seems to tell me that even with c++11 this isn't possible.

Any input?

Thanks.

Upvotes: 3

Views: 492

Answers (2)

6502
6502

Reputation: 114461

You can with some recursion trick

template<int... values>
struct myclass {
    static const unsigned char x[sizeof...(values)];
};

template<int... values>
const unsigned char myclass<values...>::x[sizeof...(values)] = { values... };

template<int count, int... values>
struct iota_array {
    typedef typename iota_array<count-1, count-1, values...>::value value;
};

template<int... values>
struct iota_array<0, values...> {
    typedef myclass<values...> value;
};

typename iota_array<E>::value myinstance;

The fact that you CAN of course doesn't mean that you should.

Upvotes: 0

Casey
Casey

Reputation: 42554

If you don't mind storing a std::array instead of a C array, this is pretty simple with an integer sequence:

template <int...I>
struct indices {};
template <int N, int...I>
struct make_indices : make_indices<N-1, N-1, I...> {};
template <int...I>
struct make_indices<0, I...> : indices<I...> {};

template <typename T, int...I>
constexpr std::array<T, sizeof...(I)>
iota_array_helper(indices<I...>) {
  return {I...};
}

template <typename T, std::size_t N>
constexpr std::array<T, N>
iota_array() {
  return iota_array_helper<T>(make_indices<N>());
}

which you can use as:

const unsigned E = 256;

class A {
    public:
        static const std::array<unsigned, E> x;
    ...
};

std::array<unsigned, E> A::x = iota_array<unsigned, E>();

Here it is live.

Upvotes: 2

Related Questions