- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
模板类std::common_type
将通用类型计算为可变参数类型列表。 It is defined using the return type of the ternary operator x:y?z
recursively.从那个定义来看,对我来说并不明显,是否计算 std::common_type<X,Y>
是关联的,i。 e.是否
using namespace std;
static_assert( is_same<common_type< X, common_type<Y,Z>::type >::type,
common_type< common_type<X,Y>::type, Z >::type>::value, "" );
不会对所有类型抛出编译时错误X
, Y
和 Z
is_same<...>
表达式有效。
请注意,我不是在问是否
static_assert( is_same<common_type<X,Y>::type,
common_type<Y,X>::type>::value, "" );
永远不会开火。显然不会。以上是一个完全不同的问题。
另请注意,std::common_type
的规范在 C++14 中略有变化,可能会在 C++17 中再次发生变化。因此,对于标准的不同版本,答案可能会有所不同。
最佳答案
这在 MinGW-w64(gcc 4.9.1) 上失败。在 VS2013 和 gcc5.2 上(感谢 Baum mit Augen)或带有 libc++ 的 clang 3.7 上也失败。
#include <type_traits>
using namespace std;
struct Z;
struct X{operator Z();};
struct Y{operator X();};
struct Z{operator Y();};
static_assert( is_same<common_type<X,Y>::type,
common_type<Y,X>::type>::value, "" ); // PASS
static_assert( is_same<common_type<X,Z>::type,
common_type<Z,X>::type>::value, "" ); // PASS
static_assert( is_same<common_type<Y,Z>::type,
common_type<Z,Y>::type>::value, "" ); // PASS
static_assert( is_same<common_type< X, common_type<Y,Z>::type >::type,
common_type< common_type<X,Y>::type, Z >::type>::value, "" ); // FAIL...
关于c++ - `std::common_type` 是关联的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414139/
我想知道如何 std::visit返回类型转换应该有效。 上下文如下:我有一个变体对象,我想根据其基础类型应用(通过 std::visit )不同的函数。每个函数的结果可能有不同的类型,但我希望 st
在 Nicolai M. Josuttis 的“The Cpp standard library”一书中,第 2 版中说(5.4,p.125)struct common 类型的定义如下: templa
为了了解它是如何工作的,我查看了标题 type_traits 中 std::common_type 的 libstdc++ 实现。我不得不承认我并不真正理解它是如何工作的。在这里: /// commo
模板类std::common_type将通用类型计算为可变参数类型列表。 It is defined using the return type of the ternary operator x:y
我开始查看 std::common_type并且不确定它的用途和功能。有几件事让我觉得很奇怪: 参数的顺序很重要:common_type可能与 common_type 不同.一个可能编译,另一个可能不
我不确定如何描述模板的类型何时是结构本身,如下所示。 template struct Point{}; Point> p; 这是定义的行为吗?如果是这样,我不知道实现它的最佳方法,以便我可以返回一个没
我想编写一个辅助模板来检查模板参数包是否具有通用类型,即,是否将 std::common_type 应用于包定义了一个类型。 使用 std::void_t在 SFINAE 中,我得出了以下定义: te
自 C++11 以来,引入了类型特征 std::common_type。 std::common_type 确定其所有模板参数之间的公共(public)类型。在 C++14 中,还引入了辅助类型 st
我注意到 std::chrono::duration的一元 + 和 - 运算符在 libcxx 中实现返回类型为 typename common_type::type .为什么不是简单的duratio
以下是 std::common_type 的专业之一. template struct common_type { typedef decltype(true ? declval() : d
此代码在 VC11 下为两个检查打印出“unsigned short”,但在 VC10 下第一个检查打印“int”。我总是期待 std::common_type::type成为T .这是错误还是允许的
我很困惑:升级到 GCC 6 (RC1) 后,一些使用 std::common_type 的模板代码在失败之前有效。我尝试了clang,但也失败了......所以我一定做错了什么! 代码相当于: #i
用std::declval不是更好吗?声明形式: template T declval(); // (1) 然后是当前的: template T && declval(); // (2) std::c
用 C++17 标记,因为这是我引用的规范版本。 这是 IMO,标准的相关部分,第 23.15.7.6 节 [meta.trans.other]。 template struct common_ty
给定类型 A,B , 我关心 std::common_type 的确切定义, 不考虑可变参数 std::common_type对于任意类型 A... .所以让 using T = decltype(t
我正在将我的部分代码库从 C++11 升级到 C++14。我有几个数学实用函数,它们接受多个输入参数并返回一个 std::common_type_t 类型的值。 . 我正在考虑用一个简单的 auto
作为对我的 last question 的回答建议尽可能使用 std::common_type::type在自动返回类型的声明中而不是我原来的 decltype() .但是,这样做我遇到了问题(使用
common_type::type是 unsigned long因为关于积分提升后的操作数,标准说... [...] if the operand that has unsigned integer
我是一名优秀的程序员,十分优秀!