我知道 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/