gpt4 book ai didi

c++ - `std::common_type` 是关联的吗?

转载 作者:IT老高 更新时间:2023-10-28 22:20:24 25 4
gpt4 key购买 nike

模板类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 , YZ 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com