1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify -pedantic %s
// Test the C++0x-specific reference initialization rules, e.g., the
// rules for rvalue references.
template<typename T> T prvalue();
template<typename T> T&& xvalue();
template<typename T> T& lvalue();
struct Base { };
struct Derived : Base { };
struct HasArray {
int array[5];
};
int f(int);
template<typename T>
struct ConvertsTo {
operator T(); // expected-note 4{{candidate function}}
};
void test_rvalue_refs() {
// If the initializer expression...
// - is an xvalue, class prvalue, array prvalue or function lvalue
// and "cv1 T1" is reference-compatible with "cv2 T2", or
// xvalue case
Base&& base0 = xvalue<Base>();
Base&& base1 = xvalue<Derived>();
int&& int0 = xvalue<int>();
// class prvalue case
Base&& base2 = prvalue<Base>();
Base&& base3 = prvalue<Derived>();
// array prvalue case
int (&&array0)[5] = HasArray().array;
// function lvalue case
int (&&function0)(int) = f;
// - has a class type (i.e., T2 is a class type), where T1 is not
// reference-related to T2, and can be implicitly converted to
// an xvalue, class prvalue, or function lvalue of type "cv3
// T3", where "cv1 T1" is reference-compatible with "cv3 T3",
// xvalue
Base&& base4 = ConvertsTo<Base&&>();
Base&& base5 = ConvertsTo<Derived&&>();
int && int1 = ConvertsTo<int&&>();
// class prvalue
Base&& base6 = ConvertsTo<Base>();
Base&& base7 = ConvertsTo<Derived>();
// function lvalue
int (&&function1)(int) = ConvertsTo<int(&)(int)>();
// In the second case, if the reference is an rvalue reference and
// the second standard conversion sequence of the user-defined
// conversion sequence includes an lvalue-to-rvalue conversion, the
// program is ill-formed.
int &&int2 = ConvertsTo<int&>(); // expected-error{{no viable conversion from 'ConvertsTo<int &>' to 'int'}}
int &&int3 = ConvertsTo<float&>(); // expected-error{{no viable conversion from 'ConvertsTo<float &>' to 'int'}}
}
class NonCopyable {
NonCopyable(const NonCopyable&);
};
class NonCopyableDerived : public NonCopyable {
NonCopyableDerived(const NonCopyableDerived&);
};
// Make sure we get direct bindings with no copies.
void test_direct_binding() {
NonCopyable &&nc0 = prvalue<NonCopyable>();
NonCopyable &&nc1 = prvalue<NonCopyableDerived>();
NonCopyable &&nc2 = xvalue<NonCopyable>();
NonCopyable &&nc3 = xvalue<NonCopyableDerived>();
const NonCopyable &nc4 = prvalue<NonCopyable>();
const NonCopyable &nc5 = prvalue<NonCopyableDerived>();
const NonCopyable &nc6 = xvalue<NonCopyable>();
const NonCopyable &nc7 = xvalue<NonCopyableDerived>();
NonCopyable &&nc8 = ConvertsTo<NonCopyable&&>();
NonCopyable &&nc9 = ConvertsTo<NonCopyableDerived&&>();
const NonCopyable &nc10 = ConvertsTo<NonCopyable&&>();
const NonCopyable &nc11 = ConvertsTo<NonCopyableDerived&&>();
}
namespace std_example_1 {
double d = 2.0;
double& rd = d;
const double& rcd = d;
struct A { };
struct B : A {
operator int&();
} b;
A& ra = b;
const A& rca = b;
int& ir = B();
}
namespace std_example_2 {
double& rd2 = 2.0; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a temporary of type 'double'}}
int i = 2;
double& rd3 = i; // expected-error{{non-const lvalue reference to type 'double' cannot bind to a value of unrelated type 'int'}}
struct A { };
struct B : A { } b;
extern B f();
const A& rca = f();
A&& rra = f();
struct X {
operator B(); // expected-note{{candidate function}}
operator int&(); // expected-note{{candidate function}}
} x;
const A& r = x;
int&& rri = static_cast<int&&>(i);
B&& rrb = x;
int&& rri2 = X(); // expected-error{{no viable conversion from 'std_example_2::X' to 'int'}}
const double& rcd2 = 2;
double&& rrd = 2;
const volatile int cvi = 1;
const int& r2 = cvi; // expected-error{{binding of reference to type 'const int' to a value of type 'const volatile int' drops qualifiers}}
double d;
double&& rrd2 = d; // expected-error{{rvalue reference to type 'double' cannot bind to lvalue of type 'double'}}
double&& rrd3 = i;
}
namespace argument_passing {
void base_rvalue_ref(Base&&);
void int_rvalue_ref(int&&); // expected-note 2{{passing argument to parameter here}}
void array_rvalue_ref(int (&&)[5]);
void function_rvalue_ref(int (&&)(int));
void test() {
base_rvalue_ref(xvalue<Base>());
base_rvalue_ref(xvalue<Derived>());
int_rvalue_ref(xvalue<int>());
base_rvalue_ref(prvalue<Base>());
base_rvalue_ref(prvalue<Derived>());
array_rvalue_ref(HasArray().array);
function_rvalue_ref(f);
base_rvalue_ref(ConvertsTo<Base&&>());
base_rvalue_ref(ConvertsTo<Derived&&>());
int_rvalue_ref(ConvertsTo<int&&>());
base_rvalue_ref(ConvertsTo<Base>());
base_rvalue_ref(ConvertsTo<Derived>());
function_rvalue_ref(ConvertsTo<int(&)(int)>());
int_rvalue_ref(ConvertsTo<int&>()); // expected-error{{no viable conversion from 'ConvertsTo<int &>' to 'int'}}
int_rvalue_ref(ConvertsTo<float&>()); // expected-error{{no viable conversion from 'ConvertsTo<float &>' to 'int'}}
}
}
|