Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is_same compilation failed when use enable_if, but is_same_v pass

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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").

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading