5

Both are almost on the latest versions of g++ and clang++ in Ubuntu 14.04. The below code compiles fine with g++.

template<typename T>
struct B { static const int i = T::value; };

struct D : B<D> { static const int value = 0; } d;

int main () {}

However, with clang++ (even with -std=c++11), below error is thrown:

error: no member named 'value' in 'D'
struct B { static const int i = T::value; };
                                ~~~^
note: in instantiation of template class 'B<D>' requested here
struct D : B<D> { static const int value = 0; } d;

Is it a non-standard code?

Above example is just a snippet of a bigger code which has to be ported to iOS/Android. iOS supports clang and I am not sure, how it will work out.
Any help is appreciated.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    Ah, the fact that `i` is unused allows GCC to evaluate `T::value` later. Adding `int j[i + 1];` to `struct B` makes GCC reject the code too, even though `i + 1` would otherwise be a constant expression with a positive value. So it's the same issue after all. –  Jul 19 '14 at 11:12
  • For those who face this 'issue' should *define* `i` outside the `struct B`. In the same way, if you are using `T::value` for any other data member then define the body of that function outside the `struct B`. [This code](http://ideone.com/xSFbPg) will also compile fine with clang++ – iammilind Jul 19 '14 at 12:00

0 Answers0