我们可以将非类的prvalue强制转换为xvalue吗?

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++-7clang-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

  • 此外,上面示例源自的同一段内容如下:

    对于两个相似的类型T1T2,类型的prvalue T1可以明确地转化为式T2使用const_cast。a的结果const_cast引用原始实体

    将其与它后面的一个比较(7.6.1.10,第4段[expr.const.cast]):

    两个对象类型T1T2,如果一个指针T1可以显式转换到类型“指针T2”使用 const_cast,然后进行以下转换也可以由:

    • 左值类型T1可以显式转换为左值类型的T2使用铸造const_cast<T2&>;
    • 类型类型的glvalueT1可以使用cast 显式地转换为类型的xvalue ; 和T2const_cast<T2&&>
    • 如果T1是一个类类型,一个prvalue类型T1可以显式转换为x值类型的T2使用铸造const_cast<T2&&>

    如果操作数是glvalue,则引用const_cast的结果将引用原始对象,否则将引用临时实现转换的结果

    这种对比似乎暗示着,从非类prvaluexvalue的 转换不会遭受临时的物化转换,这对我来说很奇怪。

那么,以上示例的目的是什么?