Let's say, given C++17's if constexpr
and Concepts TS (for instance, in recent gcc versions), we'd like to check if a type in a template function has a nested type:
#include <iostream>
struct Foo { using Bar = int; };
template<typename T>
void doSmth(T)
{
if constexpr (requires { typename T::Bar; })
std::cout << "has nested! " << typename T::Bar {} << std::endl;
else
std::cout << "no nested!" << std::endl;
}
int main()
{
doSmth(Foo {});
//doSmth(0);
}
The documentation for concepts is scarce, so I might have got it wrong, but seems like that's it (and the live example is on Wandbox).
Now let's consider what should happen when uncommenting the other doSmth
call. It seems reasonable to expect that the requires-clause would evaluate to false
, and the else
branch of the if constexpr
will be taken. Contrary to that, gcc makes this a hard error:
prog.cc: In instantiation of 'void doSmth(T) [with T = int]':
prog.cc:17:13: required from here
prog.cc:8:5: error: 'int' is not a class, struct, or union type
if constexpr (requires { typename T::Bar; })
^~
Is that a bug in gcc, or is that the intended behaviour?