Skip to content

Commit 1b74658

Browse files
committed
Decay return type of r | x when r is result<T&>. Refs #128.
1 parent 6dc7819 commit 1b74658

File tree

7 files changed

+122
-89
lines changed

7 files changed

+122
-89
lines changed

include/boost/system/result.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,10 @@ template<class T, class E> struct is_result< result<T, E> >: std::true_type {};
919919
// result | value
920920

921921
template<class T, class E, class U,
922-
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
922+
class En = typename std::enable_if<std::is_convertible<U, typename std::decay<T>::type>::value>::type
923923
>
924-
T operator|( result<T, E> const& r, U&& u )
924+
typename std::decay<T>::type
925+
operator|( result<T, E> const& r, U&& u )
925926
{
926927
if( r )
927928
{
@@ -934,9 +935,10 @@ T operator|( result<T, E> const& r, U&& u )
934935
}
935936

936937
template<class T, class E, class U,
937-
class En = typename std::enable_if<detail::is_value_convertible_to<U, T>::value>::type
938+
class En = typename std::enable_if<std::is_convertible<U, typename std::decay<T>::type>::value>::type
938939
>
939-
T operator|( result<T, E>&& r, U&& u )
940+
typename std::decay<T>::type
941+
operator|( result<T, E>&& r, U&& u )
940942
{
941943
if( r )
942944
{

test/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ boost_test(TYPE run SOURCES result_value_construct6.cpp)
165165
boost_test(TYPE run SOURCES result_value_construct7.cpp)
166166
boost_test(TYPE run SOURCES result_error_construct5.cpp)
167167
boost_test(TYPE run SOURCES result_or_value.cpp)
168-
boost_test(TYPE compile-fail SOURCES result_or_value_fail.cpp)
169-
boost_test(TYPE compile-fail SOURCES result_or_value_fail2.cpp)
170168
boost_test(TYPE run SOURCES result_or_fn0v.cpp)
171169
boost_test(TYPE run SOURCES result_or_fn0r.cpp)
172170
boost_test(TYPE run SOURCES result_and_fn1v.cpp)

test/Jamfile.v2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ run result_value_construct6.cpp ;
198198
run result_value_construct7.cpp ;
199199
run result_error_construct5.cpp ;
200200
run result_or_value.cpp ;
201-
compile-fail result_or_value_fail.cpp ;
202-
compile-fail result_or_value_fail2.cpp ;
203201
run result_or_fn0v.cpp ;
204202
run result_or_fn0r.cpp ;
205203
run result_and_fn1v.cpp ;

test/result_or_fn0r.cpp

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -191,120 +191,102 @@ int main()
191191

192192
{
193193
int x1 = 1;
194-
int x3 = 3;
195194

196195
result<int&> r( x1 );
197196

198-
int& x = r | fri | x3;
197+
auto r2 = r | fri;
199198

200-
BOOST_TEST_EQ( &x, &x1 );
199+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
201200
}
202201

203202
{
204203
int x1 = 1;
205-
int x3 = 3;
206204

207205
result<int&> const r( x1 );
208206

209-
int& x = r | fri | x3;
207+
auto r2 = r | fri;
210208

211-
BOOST_TEST_EQ( &x, &x1 );
209+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
212210
}
213211

214212
{
215213
int x1 = 1;
216-
int x3 = 3;
217214

218-
int& x = result<int&>( x1 ) | fri | x3;
215+
auto r2 = result<int&>( x1 ) | fri;
219216

220-
BOOST_TEST_EQ( &x, &x1 );
217+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
221218
}
222219

223220
{
224221
int x1 = 1;
225-
int x3 = 3;
226222

227223
result<int&> r( x1 );
228224

229-
int& x = r | fri2 | x3;
225+
auto r2 = r | fri2;
230226

231-
BOOST_TEST_EQ( &x, &x1 );
227+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
232228
}
233229

234230
{
235231
int x1 = 1;
236-
int x3 = 3;
237232

238233
result<int&> const r( x1 );
239234

240-
int& x = r | fri2 | x3;
235+
auto r2 = r | fri2;
241236

242-
BOOST_TEST_EQ( &x, &x1 );
237+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
243238
}
244239

245240
{
246241
int x1 = 1;
247-
int x3 = 3;
248242

249-
int& x = result<int&>( x1 ) | fri2 | x3;
243+
auto r2 = result<int&>( x1 ) | fri2;
250244

251-
BOOST_TEST_EQ( &x, &x1 );
245+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &x1 );
252246
}
253247

254248
{
255-
int x3 = 3;
256-
257249
result<int&, E> r( in_place_error );
258250

259-
int& x = r | fri | x3;
251+
auto r2 = r | fri;
260252

261-
BOOST_TEST_EQ( &x, &*fri() );
253+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &*fri() );
262254
}
263255

264256
{
265-
int x3 = 3;
266-
267257
result<int&, E> const r( in_place_error );
268258

269-
int& x = r | fri | x3;
259+
auto r2 = r | fri;
270260

271-
BOOST_TEST_EQ( &x, &*fri() );
261+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &*fri() );
272262
}
273263

274264
{
275-
int x3 = 3;
276-
277-
int& x = result<int&, E>( in_place_error ) | fri | x3;
265+
auto r2 = result<int&, E>( in_place_error ) | fri;
278266

279-
BOOST_TEST_EQ( &x, &*fri() );
267+
BOOST_TEST( r2.has_value() ) && BOOST_TEST_EQ( &*r2, &*fri() );
280268
}
281269

282270
{
283-
int x3 = 3;
284-
285271
result<int&, E> r( in_place_error );
286272

287-
int& x = r | fri2 | x3;
273+
auto r2 = r | fri2;
288274

289-
BOOST_TEST_EQ( &x, &x3 );
275+
BOOST_TEST( r2.has_error() );
290276
}
291277

292278
{
293-
int x3 = 3;
294-
295279
result<int&, E> const r( in_place_error );
296280

297-
int& x = r | fri2 | x3;
281+
auto r2 = r | fri2;
298282

299-
BOOST_TEST_EQ( &x, &x3 );
283+
BOOST_TEST( r2.has_error() );
300284
}
301285

302286
{
303-
int x3 = 3;
287+
auto r2 = result<int&, E>( in_place_error ) | fri2;
304288

305-
int& x = result<int&, E>( in_place_error ) | fri2 | x3;
306-
307-
BOOST_TEST_EQ( &x, &x3 );
289+
BOOST_TEST( r2.has_error() );
308290
}
309291

310292
{

test/result_or_value.cpp

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <boost/system/result.hpp>
66
#include <boost/core/lightweight_test.hpp>
7+
#include <boost/core/lightweight_test_trait.hpp>
8+
#include <type_traits>
79

810
using namespace boost::system;
911

@@ -110,9 +112,22 @@ int main()
110112

111113
result<int&> r( x1 );
112114

113-
int& x = r | x2;
115+
{
116+
auto&& x3 = r | x2;
114117

115-
BOOST_TEST_EQ( &x, &x1 );
118+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
119+
120+
BOOST_TEST_EQ( x3, x1 );
121+
BOOST_TEST_NE( &x3, &x1 );
122+
}
123+
124+
{
125+
auto&& x4 = r | 3;
126+
127+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
128+
129+
BOOST_TEST_EQ( x4, x1 );
130+
}
116131
}
117132

118133
{
@@ -121,46 +136,111 @@ int main()
121136

122137
result<int&> const r( x1 );
123138

124-
int& x = r | x2;
139+
{
140+
auto&& x3 = r | x2;
141+
142+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
143+
144+
BOOST_TEST_EQ( x3, x1 );
145+
BOOST_TEST_NE( &x3, &x1 );
146+
}
125147

126-
BOOST_TEST_EQ( &x, &x1 );
148+
{
149+
auto&& x4 = r | 3;
150+
151+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
152+
153+
BOOST_TEST_EQ( x4, x1 );
154+
}
127155
}
128156

129157
{
130158
int x1 = 1;
131159
int x2 = 2;
132160

133-
int& x = result<int&>( x1 ) | x2;
161+
{
162+
auto&& x3 = result<int&>( x1 ) | x2;
163+
164+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
165+
166+
BOOST_TEST_EQ( x3, x1 );
167+
BOOST_TEST_NE( &x3, &x1 );
168+
}
169+
170+
{
171+
auto&& x4 = result<int&>( x1 ) | 3;
172+
173+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
134174

135-
BOOST_TEST_EQ( &x, &x1 );
175+
BOOST_TEST_EQ( x4, x1 );
176+
}
136177
}
137178

138179
{
139180
int x2 = 2;
140181

141182
result<int&, E> r( in_place_error );
142183

143-
int& x = r | x2;
184+
{
185+
auto&& x3 = r | x2;
144186

145-
BOOST_TEST_EQ( &x, &x2 );
187+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
188+
189+
BOOST_TEST_EQ( x3, x2 );
190+
BOOST_TEST_NE( &x3, &x2 );
191+
}
192+
193+
{
194+
auto&& x4 = r | 3;
195+
196+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
197+
198+
BOOST_TEST_EQ( x4, 3 );
199+
}
146200
}
147201

148202
{
149203
int x2 = 2;
150204

151205
result<int&, E> const r( in_place_error );
152206

153-
int& x = r | x2;
207+
{
208+
auto&& x3 = r | x2;
209+
210+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
211+
212+
BOOST_TEST_EQ( x3, x2 );
213+
BOOST_TEST_NE( &x3, &x2 );
214+
}
154215

155-
BOOST_TEST_EQ( &x, &x2 );
216+
{
217+
auto&& x4 = r | 3;
218+
219+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
220+
221+
BOOST_TEST_EQ( x4, 3 );
222+
}
156223
}
157224

158225
{
159226
int x2 = 2;
160227

161-
int& x = result<int&, E>( in_place_error ) | x2;
228+
{
229+
auto&& x3 = result<int&, E>( in_place_error ) | x2;
230+
231+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype(x3)> ));
232+
233+
BOOST_TEST_EQ( x3, x2 );
234+
BOOST_TEST_NE( &x3, &x2 );
235+
}
236+
237+
{
238+
auto&& x4 = result<int&, E>( in_place_error ) | 3;
239+
240+
BOOST_TEST_TRAIT_FALSE(( std::is_lvalue_reference<decltype( x4 )> ));
162241

163-
BOOST_TEST_EQ( &x, &x2 );
242+
BOOST_TEST_EQ( x4, 3 );
243+
}
164244
}
165245

166246
return boost::report_errors();

test/result_or_value_fail.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/result_or_value_fail2.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)