1818
1919#include " test_macros.h"
2020
21- struct LVal {
22- constexpr std::optional<int > operator ()(int &) { return 1 ; }
23- std::optional<int > operator ()(const int &) = delete;
24- std::optional<int > operator ()(int &&) = delete;
25- std::optional<int > operator ()(const int &&) = delete;
26- };
27-
28- struct CLVal {
29- std::optional<int > operator ()(int &) = delete;
30- constexpr std::optional<int > operator ()(const int &) { return 1 ; }
31- std::optional<int > operator ()(int &&) = delete;
32- std::optional<int > operator ()(const int &&) = delete;
33- };
34-
35- struct RVal {
36- std::optional<int > operator ()(int &) = delete;
37- std::optional<int > operator ()(const int &) = delete;
38- constexpr std::optional<int > operator ()(int &&) { return 1 ; }
39- std::optional<int > operator ()(const int &&) = delete;
40- };
41-
42- struct CRVal {
43- std::optional<int > operator ()(int &) = delete;
44- std::optional<int > operator ()(const int &) = delete;
45- std::optional<int > operator ()(int &&) = delete;
46- constexpr std::optional<int > operator ()(const int &&) { return 1 ; }
47- };
48-
4921void test () {
22+ // [optional.bad.access]
23+
5024 std::bad_optional_access ex;
5125
5226 ex.what (); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
5327
28+ // [optional.optional]
29+
5430 std::optional<int > opt;
5531 const std::optional<int > cOpt;
5632
@@ -74,39 +50,41 @@ void test() {
7450 std::move (cOpt).value (); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
7551
7652 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
77- opt.value_or (82 );
53+ opt.value_or (94 );
7854 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
79- cOpt.value_or (82 );
55+ cOpt.value_or (94 );
8056 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
81- std::move (opt).value_or (82 );
57+ std::move (opt).value_or (94 );
8258 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
83- std::move (cOpt).value_or (82 );
59+ std::move (cOpt).value_or (94 );
8460
8561#if TEST_STD_VER >= 23
8662 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
87- opt.and_then (LVal{ });
63+ opt.and_then ([]( int &) { return std::optional< int >{ 82 }; });
8864 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
89- cOpt.and_then (CLVal{ });
65+ cOpt.and_then ([]( const int &) { return std::optional< int >{ 82 }; });
9066 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
91- std::move (opt).and_then (RVal{ });
67+ std::move (opt).and_then ([]( int &&) { return std::optional< int >{ 82 }; });
9268 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
93- std::move (cOpt).and_then (CRVal{ });
69+ std::move (cOpt).and_then ([]( const int &&) { return std::optional< int >{ 82 }; });
9470
9571 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
96- opt.transform (LVal{ });
72+ opt.transform ([]( int &) { return std::optional< int >{ 94 }; });
9773 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
98- cOpt.transform (CLVal{ });
74+ cOpt.transform ([]( const int &) { return std::optional< int >{ 94 }; });
9975 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
100- std::move (opt).transform (RVal{ });
76+ std::move (opt).transform ([]( int &&) { return std::optional< int >{ 94 }; });
10177 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
102- std::move (cOpt).transform (CRVal{ });
78+ std::move (cOpt).transform ([]( const int &&) { return std::optional< int >{ 94 }; });
10379
10480 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
10581 opt.or_else ([] { return std::optional<int >{82 }; });
10682 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
10783 std::move (opt).or_else ([] { return std::optional<int >{82 }; });
10884#endif // TEST_STD_VER >= 23
10985
86+ // [optional.optional.ref]
87+
11088#if TEST_STD_VER >= 26
11189 int z = 94 ;
11290 std::optional<int &> optRef{z};
@@ -128,9 +106,9 @@ void test() {
128106 cOptRef.value_or (z);
129107
130108 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
131- optRef.and_then ([](int &) { return std::optional<int >{82 }; });
109+ optRef.and_then ([](int &) { return std::optional<int >{94 }; });
132110 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
133- cOptRef.and_then ([](int &) { return std::optional<int >{82 }; });
111+ cOptRef.and_then ([](int &) { return std::optional<int >{94 }; });
134112
135113 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
136114 optRef.transform ([](int &) { return std::optional<int >{82 }; });
@@ -143,13 +121,17 @@ void test() {
143121 std::move (optRef).or_else ([] { return std::optional<int &>{}; });
144122#endif // TEST_STD_VER >= 26
145123
124+ // [optional.specalg]
125+
146126 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
147127 std::make_optional (82 );
148128 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
149129 std::make_optional<int >(' h' );
150130 // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
151131 std::make_optional<std::string>({' z' , ' m' , ' t' });
152132
133+ // [optional.hash]
134+
153135 std::hash<std::optional<int >> hash;
154136
155137 hash (opt); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
0 commit comments