Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 6850faf

Browse files
committed
PR9546, DR1268: A prvalue cannot be reinterpret_cast to an rvalue reference
type. But a glvalue can be reinterpret_cast to either flavor of reference. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155789 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e531001 commit 6850faf

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lib/Sema/SemaCast.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,9 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr,
15041504
}
15051505

15061506
if (const ReferenceType *DestTypeTmp = DestType->getAs<ReferenceType>()) {
1507-
bool LValue = DestTypeTmp->isLValueReferenceType();
1508-
if (LValue && !SrcExpr.get()->isLValue()) {
1509-
// Cannot cast non-lvalue to lvalue reference type. See the similar
1510-
// comment in const_cast.
1507+
if (!SrcExpr.get()->isGLValue()) {
1508+
// Cannot cast non-glvalue to (lvalue or rvalue) reference type. See the
1509+
// similar comment in const_cast.
15111510
msg = diag::err_bad_cxx_cast_rvalue;
15121511
return TC_NotApplicable;
15131512
}

test/CXX/expr/expr.post/expr.reinterpret.cast/p1-0x.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ template<typename T> T&& xvalue();
1010
void test_classification(char *ptr) {
1111
int (&fr0)(int) = reinterpret_cast<int (&&)(int)>(f);
1212
int &&ir0 = reinterpret_cast<int &&>(*ptr);
13-
int &&ir1 = reinterpret_cast<int &&>(0);
14-
int &&ir2 = reinterpret_cast<int &&>('a');
13+
int &&ir1 = reinterpret_cast<int &&>(0); // expected-error {{rvalue to reference type}}
14+
int &&ir2 = reinterpret_cast<int &&>('a'); // expected-error {{rvalue to reference type}}
1515
int &&ir3 = reinterpret_cast<int &&>(xvalue<char>());
16+
// Per DR1268, reinterpret_cast can convert between lvalues and xvalues.
17+
int &ir4 = reinterpret_cast<int &>(xvalue<char>());
18+
int &&ir5 = reinterpret_cast<int &&>(*ptr);
1619
}

0 commit comments

Comments
 (0)