Mat*_*t G 20 c++ templates c++11
我很困惑:在升级到GCC 6(RC1)之前,一些模板代码使用std::common_type了失败前工作的代码.我试过clang,那也失败了......所以我一定做错了!
代码相当于:
#include <type_traits>
#include <typeinfo>
using namespace std;
// common_type of two const type_info& is ok (compiles ok)
common_type<const type_info&, const type_info&>::type func1();
// common_type of three type_info& is bad...(fails to compile)
common_type<const type_info&, const type_info&, const type_info&>::type func2();
// common_type of two const int& is ok
common_type<const int&, const int&>::type func3();
// common_type of three const int& is ok too!
common_type<const int&, const int&, const int&>::type func4();
Run Code Online (Sandbox Code Playgroud)
common_type具有三个类型参数的第二个std::type_info const &无法编译.clang密码建议我使用两个参数std::common_type,但这是在模板扩展中我无法控制输入!
这看起来很奇怪:为什么const type_info&3 的情况会失败,但其他任何看似等效的类型都不会失败?
T.C*_*.C. 15
首先common_type_t<T1, T2>是(大致)std::decay_t<decltype(true? std::declval<T1>() : std::declval<T2>())>.它会衰减类型 - 剥离参考,删除顶级cv资格,并执行数组到指针和函数到指针的转换.
所以,common_type<const type_info&, const type_info&>::type是的type_info.虽然func1声明似乎有效,但在编写其定义时会遇到严重问题.
common_type_t<T1, T2, T3>是common_type_t<common_type_t<T1, T2>, T3>,这样common_type<const type_info&, const type_info&, const type_info&>::type的common_type<type_info, const type_info&>::type.
这导致了一个混合值类别的三元表达式,根据[expr.cond]中的规则,它将尝试type_info从所选的操作数中暂时生成 - 这不起作用,因为type_info删除了复制构造函数.
在SFINAE友好的实现中,这导致common_type<const type_info&, const type_info&, const type_info&>没有成员type.如果您使用非SFINAE友好的实现,您将收到一个硬错误.
| 归档时间: |
|
| 查看次数: |
291 次 |
| 最近记录: |