1// Copyright 2021 Peter Dimov.
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#include <boost/system/result.hpp>
6#include <boost/core/lightweight_test.hpp>
7#include <string>
8
9using namespace boost::system;
10
11struct X
12{
13 int a, b;
14};
15
16result<X> fx1()
17{
18 return {{ EINVAL, generic_category() }};
19}
20
21struct Y
22{
23 std::string v;
24};
25
26struct E
27{
28 int v;
29};
30
31result<Y, E> fy1()
32{
33 return {{ .v: 42 }};
34}
35
36int main()
37{
38 {
39 result<X> r = fx1();
40
41 BOOST_TEST( !r.has_value() );
42 BOOST_TEST( r.has_error() );
43
44 BOOST_TEST_EQ( r.error(), error_code( EINVAL, generic_category() ) );
45 }
46
47 {
48 result<Y, E> r = fy1();
49
50 BOOST_TEST( !r.has_value() );
51 BOOST_TEST( r.has_error() );
52
53 BOOST_TEST_EQ( r.error().v, 42 );
54 }
55
56 return boost::report_errors();
57}
58

source code of boost/libs/system/test/result_error_construct2.cpp