C++ 我们可以将非类prvalue强制转换为xvalue吗?
考虑7.6.1.10第3段[expr.const.cast]()中的示例: 这基本上是说,我们不能将非类prvalue(=cast)具体化为xvalueC++ 我们可以将非类prvalue强制转换为xvalue吗?,c++,casting,language-lawyer,temporary,const-cast,C++,Casting,Language Lawyer,Temporary,Const Cast,考虑7.6.1.10第3段[expr.const.cast]()中的示例: 这基本上是说,我们不能将非类prvalue(=cast)具体化为xvalue 此外,上述示例来源的同一段落中说: 对于两种类似类型的T1和T2,可以使用类型为T1的PR值 使用const\u cast显式转换为类型T2。结果 const\u cast指的是原始实体 将其与后面的一个进行比较(7.6.1.10,第4段[expr.const.cast]): 对于两种对象类型T1和T2,如果可以使用指向T1的指针 使用 con
T1
和T2
,可以使用类型为T1
的PR值
使用const\u cast
显式转换为类型T2
。结果
const\u cast
指的是原始实体
将其与后面的一个进行比较(7.6.1.10,第4段[expr.const.cast]):
对于两种对象类型T1
和T2
,如果可以使用指向T1
的指针
使用
const_cast
,则还可以进行以下转换:
- 使用cast
,可以将const_cast
类型的左值显式转换为T1
类型的左值李>T2
- 使用cast
,可以将const_cast
类型的glvalue显式转换为T1
类型的xvalue; 及T2
- 如果
是类类型,则类型为T1
的prvalue可以使用castT1
显式转换为类型为const\u cast
的xvalueT2
那么,上面这个例子的意图是什么?const_cast的结果是指原始实体,这个措辞显然有缺陷,因为在C++17及以上版本中,PRValue并不表示实体。@language因此,上面的例子有缺陷。谢谢。@language律师措辞没有瑕疵。您引用的措辞在第3段中,不适用于参考案例,即第4段。第4段中的措辞清楚地表明,如果操作数是glvalue,则结果仅引用原始对象。@BRAIN第3段中的第一句和第二句不相关?
typedef int *A[3]; // array of 3 pointer to int
typedef const int *const CA[3]; // array of 3 const pointer to const int
...
A &&r2 = const_cast<A&&>(CA{}); // OK
....
if (isa<RValueReferenceType>(DestTypeTmp) && SrcExpr.get()->isRValue()) {
if (!SrcType->isRecordType()) {
// Cannot const_cast non-class prvalue to rvalue reference type. But if
// this is C-style, static_cast can do this.
msg = diag::err_bad_cxx_cast_rvalue;
return TC_NotApplicable;
}
// Materialize the class prvalue so that the const_cast can bind a
// reference to it.
NeedToMaterializeTemporary = true;
}
...