1

Possible Duplicate:
C++0x static_assert and template instantiation

The following code doesn't compile in GCC due to the static_assert:

#include <ratio>
#include <iostream>

template<typename T>
class A
{
public:
    const static T a = 10;
    A() 
    {
        static_assert(false, "Can't instantiate this");
    }
};


int main() 
{
    A<int>::a;
    return 0;
}

(http://ideone.com/3opRfQ)

However, the same code compiles in Visual Studio 2012. I only get a compile error if I actually instantiate the class (also, the static_assert is triggered without instantiating the class if I make the class non-templated).

Which compiler is behaving in accordance with the standard? I think it's GCC but I can't see a clear confirmation in the standard.

Community
  • 1
  • 1
Alex Korban
  • 14,916
  • 5
  • 44
  • 55
  • An easy trick is to "fake" a dependency on the assertion value, like with `template struct always_false{ static bool const value = false; };` and then `static_assert(always_false::value, "...");`. – Xeo Jan 10 '13 at 08:18
  • This is a different situation than in the linked question because the _template_ is instantiated in my example. So my question is almost the opposite. – Alex Korban Jan 10 '13 at 19:06

0 Answers0