简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM