Home > database >  Why is_same compilation failed when use enable_if, but is_same_v pass
Why is_same compilation failed when use enable_if, but is_same_v pass

Time:01-02

environment

x86-64 gcc 12.2, where you can try at https://godbolt.org/z/eTvTf7n3n and https://godbolt.org/z/4W7hM3Pzs

code

#include <type_traits>

// pass
template<std::enable_if_t<!std::is_same_v<int, int>, bool> = 0>
void f1(){
}

// Compilation failed
template<std::enable_if_t<!std::is_same<int, int>::value, bool> = 0>
void f2(){
}


int main()
{
}

question

Why function f1 and f2 behaves differently? is_same_v is just a constant time wrapper of is_same::value, I don't understand what makes the difference.

CodePudding user response:

The code is ill-formed NDR1. Both overloads are rejected by Clang and MSVC.

[temp.res.general]/6.1

The validity of a template may be checked prior to any instantiation. ... The program is ill-formed, no diagnostic required, if:

— no valid specialization can be generated for a template ... and the template is not instantiated, ...


1 NDR = "no diagnostic required", meaning the program is invalid, but the compilers are not required to catch that (not required to emit an error/warning message, aka "a diagnostic").

  • Related