c++ - 什么时候使用表达式的类型(不是类别)?

标签 c++ expression language-lawyer

我知道 c++ 中的每个表达式都有一个类别(prvalue、xvalue、lvalue..)和一个类型,根据标准草案,它永远不是引用类型(如果不是 prvalue,则可能是 cv 限定的)

5 If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.

6 If a prvalue initially has the type “cv T,” where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

鉴于 decltype 有自己的一套规则,自动推导也有不同的规则,这种“非引用表达式类型”何时重要?

最佳答案

重要的是 unevaluated expressions :

类型id:

typeid(std::cout << 0) == typeid(std::ostream);
// true

没有异常(exception):

template<class T> void f() noexcept(noexcept(T{}+T{}))

sizeof(即使是 sizeof has a specific rule,与完整表达式类型的规则不矛盾):

sizeof(std::cout << 0);
// the expression returns an std::ostream&, but its type is std::ostream

等等

关于c++ - 什么时候使用表达式的类型(不是类别)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51945691/

相关文章:

c++ - 删除 "using namespace std::rel_ops"可以改变行为吗?

c++ - 访问私有(private)类 C++ 内部的结构

c++ - 从十六进制转换为 LPCVOID 切断了地址的一半

c# - 如何将动态值转换为表达式中的类型值

c# - Entity Framework 结合来自不同实体的 lambda 表达式

c# - 如何获得表达结果?

c++ - 如果什么都不做,请检查 Makefile

c++ - c++最基本的上网方式是什么?

c++ - 减去与同一数组未定义行为无关的两个指针的基本原理是什么?

c++ - 为什么...(三点)在 catch block 中存在?