Consider this trivial test code:
class Test { public: Test() {} private: enum {BLAH = 42}; static constexpr int Magic() {return BLAH*4;} float f[Magic()]; }; int main(int argc, char ** argv) { Test t; return 0; }
When I try to compile it (under MacOS / X, using clang ++ from the latest Xcode), I get this compiler error:
Jeremys-Mac-Pro:~ jaf$ clang++ -std=c++11 ./test.cpp ./test.cpp:11:14: error: fields must have a constant size: 'variable length array in structure' extension will never be supported float f[Magic()];
Can someone explain why this is a mistake? For comparison, if I transfer the Magic () method from the Test class and make it a standalone function, it compiles as expected, but I really do not want to do this because I want to keep Magic () and BLAH if possible, is closed to the class Test
(Note: I am not trying to use variable-length arrays here, rather I am trying to declare an array whose size is determined by the calculation of the function at compile time)
source share