Static element of constexpr pattern array recursively required by itself

I want an array of static constexpr elements of the class inside the template, similar to the following code:

 struct Element { unsigned i; constexpr Element (unsigned i) : i(i) { } }; template <bool Reverse> struct Template { static constexpr Element element[] = { Element (Reverse ? 1 : 0), Element (Reverse ? 0 : 1), }; }; int main (int argc, char **argv) { return Template<true>::element[0].i; } 

Of course, the actual structure of Element more complex than in this example, but it already shows the problem. If I compile this gcc wit, I get an error about a recursive dependency:

 test.cc: In instantiation of 'constexpr Element Template<true>::element [2]': test.cc:11:27: recursively required from 'constexpr Element Template<true>::element [2]' test.cc:11:27: required from 'constexpr Element Template<true>::element [2]' test.cc:20:2: required from here test.cc:11:27: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) static constexpr Element element[] = { ^ compilation terminated. 

First of all, I am curious how to get around this error, but it will also be a glade if I can get a hint at the reason for this or why such a construction should not be valid ...

+7
c ++ 11 templates constexpr
source share
1 answer

You need to specify the size of the Element array. Element element[] not valid C ++.

 template <bool Reverse> struct Template { static constexpr Element element[2] = { Element (Reverse ? 1 : 0), Element (Reverse ? 0 : 1), }; }; 
-2
source share

All Articles