template<typename T, typename U = void> struct S { }; template<typename T> struct S<T, typename std::enable_if<std::is_integral<T>::value, void>::type> { void foo() {} }; ... S<int> i; i.foo(); S<double> d;
I would expect that the "master template" would never be created for the int case, but if I uncomment static_assert , creating an instance of S<int> will fail. Even a single typedef S<int> Si; will not be able to compile. (GCC 4.9.2 Cygwin)
What I was striving for was not that S<double> failed when calling foo() , but when creating the template itself, as well as with a significant error message. I know that I can do something like typename T::nonexistent_type t; in the main template, which will prevent the template from being created, but it will be worse than the static_assert message. (Note: placing static_assert in the function definition in the wizard template is still not running for S<int> )
Why is static_assert executed even if this template is not created? Is this a mandatory (or possibly "unspecified") standard? Is there a way to fail with static_assert as I want?
source share