void_t in parameter list works but not as return type

房东的猫 提交于 2019-12-07 10:46:59

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/28967003/void-t-in-parameter-list-works-but-not-as-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!