std::common_type with references to type_info
std::common_type with references to type_info
我很困惑:在升级到GCC 6(RC1)时,一些使用std::common_type
的模板代码失败了。我试过叮当,但也失败了。。。所以我一定做错了什么!
代码总计为:
#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();
具有三个类型为std::type_info const &
的参数的第二个common_type
编译失败。clang隐晦地建议我使用双参数std::common_type
,但这是在模板扩展中,我无法控制输入!
这似乎很奇怪:为什么3的const type_info&
案例会失败,而其他看似等效的类型却没有失败?
请参见此处:https://godbolt.org/g/Ob4y0x
首先,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友好的实现,您将得到一个硬错误。
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 是否有用于"go to variable type definition" C++中"auto"变量的 Visual Studio 键盘快捷方式?
- 如何修复" State Error (active) E0513 a value of type "const wchar_t *" cannot be assigned to an entity o
- 索引 2D 向量时'Reference to type const..'编译错误
- "How to use long long data-type rather than pointers data-type to modify other variables ?"
- 这是什么意思:"member reference type 'Human *' is a pointer; did you mean to use '->'?"
- C++ 中数组的"expression must have pointer-to-object type"是什么?
- "conversion from void to non scalar type string"是什么意思?
- 我收到一个" conversion from ‘std::string (*)[50]’ to non-scalar type ‘std::string’ requested"错误,似乎无法修复它
- 访问基类型数组成员(Int-to-type习惯用法)
- std::variant、包装类和'conversion from ... to non-scalar type ... requested'
- "a value of type " void (exeCallback::*)(int) " cannot be assigned to an entity of type " void (*)(
- 如何修复C++编译器错误"cannot convert 'Type' to 'const Type*'"?
- 在 2D 阵列分配期间对 calloc 进行"expression must have (pointer-to) function type"
- 禁用 Clang 中的"cast from pointer to smaller type uint32_t"错误
- 错误消息"incompatible types when assigning to type ‘char[100]’ from type ‘char *’"
- 收到"void* is not a pointer-to-object type"错误,但使用 XCode 时代码完美执行
- 我很困惑为什么我的 fillDeck 函数说,"assigning to 'char *' from incompatible type 'const char *"
- 在C++,我收到一条消息"error: 'void*' is not a pointer-to-object type"