0

When putting a with a hardcoded value, it is triggered even if the function it's in isn't instantiated. Is this behavior correct or do I misunderstand how static assert works?

#include <type_traits>
template <class T>
struct Helper
{
    static void do_something()
    {
        // Always fails, even when not instantiated.
        static_assert(false,
            "You must specialize this class to serialize Enums.");

        // Work around:
        static_assert(!std::is_same<T, T>::value,
            "You must specialize this class to serialize Enums.");
    }
};

Using: g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

nishantjr
  • 1,788
  • 1
  • 15
  • 39

2 Answers2

0

SFINAE works for function arguments and return type, not template parameters.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

That is because "false" does not depend on the template in any way, for the compiler this code is sure to assert. There is similar thread

static_assert fails compilation even though template function is called nowhere

Community
  • 1
  • 1