应用于三元(?:)表达式时返回decltype的类型

时间:2021-07-08 16:38:37

When I look into a code snippet for a possible implementation of std::common_type

当我查看代码片段以获得std :: common_type的可能实现时

template <class ...T> struct common_type;

template <class T>
struct common_type<T> {
    typedef decay_t<T> type;
};

template <class T, class U>
struct common_type<T, U> {
    typedef decay_t<decltype(true ? declval<T>() : declval<U>())> type;
};

template <class T, class U, class... V>
struct common_type<T, U, V...> {
    typedef common_type_t<common_type_t<T, U>, V...> type;
};

The part how to get a common type for two template argument makes me confused.It is a usage of ternary operator with decltype.

如何获得两个模板参数的通用类型的部分让我感到困惑。它是使用decltype的三元运算符。

As I known, whether to return the second or third operand is decided by the value of first operand. In this snippet, the first operand is true which means the return value of expression will always be declval<T>(). If it is what i thought which make no sense... Therefore, I have tried the following test

众所周知,是否返回第二个或第三个操作数取决于第一个操作数的值。在此片段中,第一个操作数为true,这意味着expression的返回值将始终为declval ()。如果这是我认为没有意义的...因此,我尝试了以下测试

int iii = 2;
float fff = 3.3;
std::cout << typeid(decltype(false? std::move(iii):std::move(fff))).name() << std::endl;
std::cout << typeid(decltype(std::move(iii))).name() << std::endl;
std::cout << typeid(decltype(false ? iii : fff)).name() << std::endl;
std::cout << typeid(decltype(true ? iii : fff)).name() << std::endl;

// [02:23:37][ryu@C++_test]$ g++ -std=c++14 -g common_type.cpp
// output 
// f
// i
// f
// f

Comparing with the running result, The result what i though should be like as follows

与运行结果相比,结果我应该如下所示

int iii = 2;
float fff = 3.3;
std::cout << typeid(decltype(false ? iii : fff)).name() << std::endl; // should return f;
std::cout << typeid(decltype(true ? iii : fff)).name() << std::endl;  // should return i;

Anyone when can help to explain why the running result is different ?

任何人都可以帮助解释为什么跑步结果不同?

In other words, what's the return result of decltype when it is applied on a ternary expression?

换句话说,当它应用于三元表达式时,decltype的返回结果是什么?

1 个解决方案

#1


The type of an expression is a compile-time property. The value of the first operand in a conditional expression (and hence the branch selected), is, in general, a run-time thing, so it can't possibly affect the expression's type.

表达式的类型是编译时属性。条件表达式中的第一个操作数的值(以及因此选择的分支)通常是运行时事物,因此它不可能影响表达式的类型。

Instead, a complicated set of rules (more than a page of standardese, most of which I quoted in this answer) is used to determine what the "common type" of the second and third operands is, and the conditional expression is of that type. std::common_type merely leverages the existing rules in the core language.

相反,一组复杂的规则(超过一页标准,我在这个答案中引用的大多数)用于确定第二和第三个操作数的“常见类型”是什么,条件表达式是那种类型。 std :: common_type仅利用核心语言中的现有规则。

#1


The type of an expression is a compile-time property. The value of the first operand in a conditional expression (and hence the branch selected), is, in general, a run-time thing, so it can't possibly affect the expression's type.

表达式的类型是编译时属性。条件表达式中的第一个操作数的值(以及因此选择的分支)通常是运行时事物,因此它不可能影响表达式的类型。

Instead, a complicated set of rules (more than a page of standardese, most of which I quoted in this answer) is used to determine what the "common type" of the second and third operands is, and the conditional expression is of that type. std::common_type merely leverages the existing rules in the core language.

相反,一组复杂的规则(超过一页标准,我在这个答案中引用的大多数)用于确定第二和第三个操作数的“常见类型”是什么,条件表达式是那种类型。 std :: common_type仅利用核心语言中的现有规则。