https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66921

Billy Donahue <billydonahue at google dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |billydonahue at google dot com

--- Comment #2 from Billy Donahue <billydonahue at google dot com> ---
(In reply to Vlad Petric from comment #0)
> Created attachment 36010 [details]
> straightforward file (include-less) that fails to compile
> 
> template<typename T>
> struct Holder {
>   constexpr static const int array[] = { 1, 2, 3 };
>   enum {F = array[0]};
> };
> 
> class HI: public Holder<int> {};
> 
> gcc 4.9.3/Centos linux.
> 
> $ g++ -std=c++11 ./x.cc -o x.o -c
> ./x.cc: In instantiation of ‘constexpr const int Holder<int>::array []’:
> ./x.cc:4:18:   required from ‘struct Holder<int>’
> ./x.cc:7:18:   required from here
> ./x.cc:3:30: error: initializer fails to determine size of
> ‘Holder<int>::array’
>    constexpr static const int array[] = { 1, 2, 3 };
>                               ^
> ./x.cc:3:30: error: array must be initialized with a brace-enclosed
> initializer
> ./x.cc: In instantiation of ‘struct Holder<int>’:
> ./x.cc:7:18:   required from here
> ./x.cc:4:8: error: enumerator value for ‘F’ is not an integer constant
>    enum {F = array[0]};
> 
> 
> 
> * with gcc 4.8.3, it compiles just fine.
> 
> * if I remove the template, it also compiles just fine:
> 
> struct Holder {
>   constexpr static const int array[] = { 1, 2, 3 };
>   enum {F = array[0]};
> };
> 
> class HI: public Holder {};

I little bit of trial-and-error on gcc.godbolt.org confirms this is a problem
in gcc in the v4.9 series and beyond to the current v5.2, not a problem in the
v4.8 series.
http://goo.gl/li99SI

Clang compiles it okay, FWIW.

If I explicitly size the array[3], then all gcc's will compile it fine.

template<typename T>
struct Holder {
  // builds in modern gcc as long as the 3 is specified.
  constexpr static int array[3] = { 123, 234, 345 };
  enum {F = array[0]};
};
Holder<void> inst{};

Reply via email to