Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void_t in parameter list works but not as return type

There's an example on cppreference about the using alias. This example fails because int has no member foo:

template<typename...> using void_t = void;
template<typename T> void_t<typename T::foo> f();
f<int>(); // error, int does not have a nested type foo

This is clear, but when I tried putting the void_t part in the parameter list it unexpectedly compiled:

template<typename...> using void_t = void;
template<typename T> void f(void_t<typename T::foo>);
f<int>();

It compiles on clang but not in gcc. Is this a bug?

like image 617
template boy Avatar asked Feb 10 '23 17:02

template boy


1 Answers

template<class...>struct voider{using type=void;};
template<class...Ts>using void_t=typename voider<Ts...>::type;

there is an ambiguity in the C++11 standard about whether non-used template parameters to template using aliases that are invalid types/expressions are a substitution failure or not.

gcc and clang interpreted the clause differently, which is what I think you are seeing. The above void_t should make it work the same in both gcc and clang.

like image 138
Yakk - Adam Nevraumont Avatar answered Feb 13 '23 07:02

Yakk - Adam Nevraumont