c++ - gcc 4.7 fails to use pointer template parameter sometimes -


complete (not)working example:

struct s { int i; };  template<const s* _arr> class struct_array { public:     static constexpr auto arr = _arr[0]; // works     template<int >     struct inner // line 9, without struct, works     {         }; };  constexpr const s s_objs[] = {{ 42 }};  int main() {     struct_array<s_objs> t_obj;     return 0; } 

compiled this:

g++ -std=c++11 -wall constexpr.cpp -o constexpr 

i running program ideone's gcc 4.8.1, 4.7.3 prints me:

constexpr.cpp: in instantiation of ‘class struct_array<((const s*)(& s_objs))>’: constexpr.cpp:18:30:   required here constexpr.cpp:9:16: error: lvalue required unary ‘&’ operand constexpr.cpp:9:16: error: not convert template argument ‘(const s*)(& s_objs)’ ‘const s*’ 

the last 2 lines repeated 3 times. what's reason, , there workaround use code on gcc 4.7.3?

this seems compiler bug me.

i tried example on gcc 4.1.2 (codepad), , have explicitely note variable having external linkage (const imply internal linkage unless specified otherwise, following code c++03):

struct s { int i; };  template<const s* _arr> class struct_array { public:     static const s arr;     template<int >     struct inner     {         }; };  template<const s* _arr> const s struct_array<_arr>::arr = _arr[0];  // notice 'extern' extern const s s_objs[] = {{ 42 }};  int main() {     struct_array<s_objs> t_obj;     return 0; } 

i have working on gcc 4.8.1 without c++11 enabled.

so, workaround is:

change

constexpr const s s_objs[] = ...; 

to

extern const s s_objs[] = ...; 

live example here.

if want variable static class member, have specify have external linkage:

struct data {     static const s s_objs[1]; };  extern const s data::s_objs[1] = {{ 42 }}; 

this gives me warning on gcc 4.7, not on 4.8. doesn't compile on rise4fun. so, i'm not sure whether pure standard or bug in 1 of compiler.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -