Compilation should fail in any compatible compiler.
SFINAE rules are based on declarations, not definitions. (Sorry if I use the wrong terminology here.) I mean the following:
For class / structure:
template < > struct my_struct {
For function:
template <> my_function( ) { }
In addition, the structure / function property for a given set of template arguments is also subject to SFINAE rules.
Now static_assert can only appear in regions where replacement errors are errors, so if it works, you will get a compiler error.
For example, the following invalid implementation of enable_if :
// Primary template (OK) template <bool, typename T> struct enable_if; // Specialization for true (also OK) template <typename T> struct enable_if<true, T> { using type = T; }; // Specialization for false (Wrong!) template <typename T> struct enable_if<false, T> { static_assert(std::is_same<T, T*>::value, "No SFINAE here"); // The condition is always false. // Notice also that the condition depends on T but it doesn't make any difference. };
Then try this
template <typename T> typename enable_if<std::is_integral<T>::value, int>::type test(const T &t); void test(...); int main() { std::cout << std::is_same<decltype(test(0)), int>::value << std::endl;
If you remove the enable_if specialization for false , then the code compiles and outputs
1 1
Cassio neri
source share