void_t在参数列表工作,但不作为返回类型 [英] void_t in parameter list works but not as return type

查看:96
本文介绍了void_t在参数列表工作,但不作为返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于使用别名的cppreference的示例。此示例失败,因为 int 没有成员 foo

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

这很清楚, void_t 部分在参数列表中意外编译:

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>();

它在clang上编译,但不在gcc中。这是一个错误吗?

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;

在C ++ 11标准中有一个含混不清之处,即不使用模板参数来模板使用

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和clang对该子句的解释不同,这是我认为你看到的。上面的 void_t 应该使它在gcc和clang中都是一样的。

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.

这篇关于void_t在参数列表工作,但不作为返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆