b1s*_*sub 5 c++ casting temporary const-cast language-lawyer
考虑来自7.6.1.10第3段[expr.const.cast](N4810)的示例:
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
Run Code Online (Sandbox Code Playgroud)
因此,该标准说这是合法代码,但是
没有一个g++-7
或clang-6
编译正常。
事实上,这种const_cast
产权相关评论从llvm
状态:
....
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;
}
...
Run Code Online (Sandbox Code Playgroud)
这基本上表示我们无法将非类prvalue实现(= cast)为xvalue。
此外,上面示例源自的同一段内容如下:
对于两个相似的类型
T1
和T2
,类型的prvalueT1
可以明确地转化为式T2
使用const_cast
。a的结果const_cast
引用原始实体。
将其与它后面的一个比较(7.6.1.10,第4段[expr.const.cast]):
两个对象类型
T1
和T2
,如果一个指针T1
可以显式转换到类型“指针T2
”使用const_cast
,然后进行以下转换也可以由:
- 的左值类型
T1
可以显式转换为左值类型的T2
使用铸造const_cast<T2&>
;- 类型类型的glvalue
T1
可以使用cast 显式地转换为类型的xvalue ; 和T2
const_cast<T2&&>
- 如果
T1
是一个类类型,一个prvalue类型T1
可以显式转换为x值类型的T2
使用铸造const_cast<T2&&>
。如果操作数是glvalue,则引用const_cast的结果将引用原始对象,否则将引用临时实现转换的结果。
这种对比似乎暗示着,从非类prvalue到xvalue的 转换不会遭受临时的物化转换,这对我来说很奇怪。
那么,以上示例的目的是什么?