]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/optional/doc/28_ref_optional_semantics.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / optional / doc / 28_ref_optional_semantics.qbk
CommitLineData
7c673cae
FG
1[/
2 Boost.Optional
3
4 Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
5
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9]
10
11
12[section Detailed Semantics - Optional Values]
13
14[note
15The following section contains various `assert()` which are used only to show
16the postconditions as sample code. It is not implied that the type `T` must
17support each particular expression but that if the expression is supported,
18the implied condition holds.
19]
20
21
22__SPACE__
23
24[#reference_optional_constructor]
25
26[: `optional<T>::optional() noexcept;`]
27
28* [*Effect:] Default-Constructs an `optional`.
29* [*Postconditions:] `*this` is [_uninitialized].
30* [*Notes:] T's default constructor [_is not] called.
31* [*Example:]
32``
33optional<T> def ;
34assert ( !def ) ;
35``
36
37__SPACE__
38
39[#reference_optional_constructor_none_t]
40
41[: `optional<T>::optional( none_t ) noexcept;`]
42
43* [*Effect:] Constructs an `optional` uninitialized.
44* [*Postconditions:] `*this` is [_uninitialized].
45* [*Notes:] `T`'s default constructor [_is not] called. The expression
46`boost::none` denotes an instance of `boost::none_t` that can be used as
47the parameter.
48* [*Example:]
49``
50#include <boost/none.hpp>
51optional<T> n(none) ;
52assert ( !n ) ;
53``
54
55__SPACE__
56
57[#reference_optional_constructor_value]
58
59[: `optional<T>::optional( T const& v )`]
60
61* [*Requires:] `is_copy_constructible<T>::value` is `true`.
62* [*Effect:] Directly-Constructs an `optional`.
63* [*Postconditions:] `*this` is [_initialized] and its value is a ['copy]
64of `v`.
65* [*Throws:] Whatever `T::T( T const& )` throws.
66* [*Notes: ] `T::T( T const& )` is called.
67* [*Exception Safety:] Exceptions can only be thrown during
68`T::T( T const& );` in that case, this constructor has no effect.
69* [*Example:]
70``
71T v;
72optional<T> opt(v);
73assert ( *opt == v ) ;
74``
75
76
77__SPACE__
78
79[#reference_optional_constructor_move_value]
80
81[: `optional<T>::optional( T&& v )`]
82
83* [*Requires:] `is_move_constructible<T>::value` is `true`.
84* [*Effect:] Directly-Move-Constructs an `optional`.
85* [*Postconditions:] `*this` is [_initialized] and its value is move-constructed from `v`.
86* [*Throws:] Whatever `T::T( T&& )` throws.
87* [*Notes: ] `T::T( T&& )` is called.
88* [*Exception Safety:] Exceptions can only be thrown during
89`T::T( T&& );` in that case, the state of `v` is determined by exception safety guarantees for `T::T(T&&)`.
90* [*Example:]
91``
92T v1, v2;
93optional<T> opt(std::move(v1));
94assert ( *opt == v2 ) ;
95``
96
97
98__SPACE__
99
100[#reference_optional_constructor_bool_value]
101
102[: `optional<T>::optional( bool condition, T const& v ) ;` ]
103
104* If condition is true, same as:
105
106[: `optional<T>::optional( T const& v )`]
107
108* otherwise, same as:
109
110[: `optional<T>::optional()`]
111
112
113__SPACE__
114
115[#reference_optional_constructor_optional]
116
117[: `optional<T>::optional( optional const& rhs );`]
118
119* [*Requires:] `is_copy_constructible<T>::value` is `true`.
120* [*Effect:] Copy-Constructs an `optional`.
121* [*Postconditions:] If rhs is initialized, `*this` is initialized and
122its value is a ['copy] of the value of `rhs`; else `*this` is uninitialized.
123* [*Throws:] Whatever `T::T( T const& )` throws.
124* [*Notes:] If rhs is initialized, `T::T(T const& )` is called.
125* [*Exception Safety:] Exceptions can only be thrown during
126`T::T( T const& );` in that case, this constructor has no effect.
127* [*Example:]
128``
129optional<T> uninit ;
130assert (!uninit);
131
132optional<T> uinit2 ( uninit ) ;
133assert ( uninit2 == uninit );
134
135optional<T> init( T(2) );
136assert ( *init == T(2) ) ;
137
138optional<T> init2 ( init ) ;
139assert ( init2 == init ) ;
140``
141
142
143__SPACE__
144
145[#reference_optional_move_constructor_optional]
146
147[: `optional<T>::optional( optional&& rhs ) noexcept(`['see below]`);`]
148
149* [*Requires:] `is_move_constructible<T>::value` is `true`.
150* [*Effect:] Move-constructs an `optional`.
151* [*Postconditions:] If `rhs` is initialized, `*this` is initialized and
152its value is move constructed from `rhs`; else `*this` is uninitialized.
153* [*Throws:] Whatever `T::T( T&& )` throws.
154* [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value`.
155* [*Notes:] If `rhs` is initialized, `T::T( T && )` is called.
156* [*Exception Safety:] Exceptions can only be thrown during
157`T::T( T&& );` in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety of `T::T(T&&)`.
158* [*Example:]
159``
160optional<std::unique_ptr<T>> uninit ;
161assert (!uninit);
162
163optional<std::unique_ptr<T>> uinit2 ( std::move(uninit) ) ;
164assert ( uninit2 == uninit );
165
166optional<std::unique_ptr<T>> init( std::uniqye_ptr<T>(new T(2)) );
167assert ( **init == T(2) ) ;
168
169optional<std::unique_ptr<T>> init2 ( std::move(init) ) ;
170assert ( init );
171assert ( *init == nullptr );
172assert ( init2 );
173assert ( **init2 == T(2) ) ;
174``
175
176
177__SPACE__
178
179[#reference_optional_constructor_other_optional]
180
181[: `template<U> explicit optional<T>::optional( optional<U> const& rhs );`]
182
183* [*Effect:] Copy-Constructs an `optional`.
184* [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
185value is a ['copy] of the value of rhs converted to type `T`; else `*this` is
186uninitialized.
187* [*Throws:] Whatever `T::T( U const& )` throws.
188* [*Notes: ] `T::T( U const& )` is called if `rhs` is initialized, which requires a
189valid conversion from `U` to `T`.
190* [*Exception Safety:] Exceptions can only be thrown during `T::T( U const& );`
191in that case, this constructor has no effect.
192* [*Example:]
193``
194optional<double> x(123.4);
195assert ( *x == 123.4 ) ;
196
197optional<int> y(x) ;
198assert( *y == 123 ) ;
199``
200
201__SPACE__
202
203[#reference_optional_move_constructor_other_optional]
204
205[: `template<U> explicit optional<T>::optional( optional<U>&& rhs );`]
206
207* [*Effect:] Move-constructs an `optional`.
208* [*Postconditions:] If `rhs` is initialized, `*this` is initialized and its
209value is move-constructed from `*rhs`; else `*this` is
210uninitialized.
211* [*Throws:] Whatever `T::T( U&& )` throws.
212* [*Notes: ] `T::T( U&& )` is called if `rhs` is initialized, which requires a
213valid conversion from `U` to `T`.
214* [*Exception Safety:] Exceptions can only be thrown during `T::T( U&& );`
215in that case, `rhs` remains initialized and the value of `*rhs` is determined by exception safety guarantee of `T::T( U&& )`.
216* [*Example:]
217``
218optional<double> x(123.4);
219assert ( *x == 123.4 ) ;
220
221optional<int> y(std::move(x)) ;
222assert( *y == 123 ) ;
223``
224
225__SPACE__
226
227[#reference_optional_in_place_init]
228
229[: `template<class... Args> explicit optional<T>::optional( in_place_init_t, Args&&... ars );`]
230
231* [*Requires:] `is_constructible_v<T, Args&&...>` is `true`.
232* [*Effect:] Initializes the contained value as if direct-non-list-initializing an object of type `T` with the
233arguments `std::forward<Args>(args)...`.
234* [*Postconditions:] `*this` is initialized.
235* [*Throws:] Any exception thrown by the selected constructor of `T`.
236* [*Notes: ] `T` need not be __MOVE_CONSTRUCTIBLE__. On compilers that do not suppor variadic templates or rvalue references, this constuctor is available in limited functionality. For details [link optional_emplace_workaround see here].
237
238* [*Example:]
239``
240// creates an std::mutex using its default constructor
241optional<std::mutex> om {in_place_init};
242assert (om);
243
244// creates a unique_lock by calling unique_lock(*om, std::defer_lock)
245optional<std::unique_lock<std::mutex>> ol {in_place_init, *om, std::defer_lock};
246assert (ol);
247assert (!ol->owns_lock());
248``
249
250__SPACE__
251
252[#reference_optional_in_place_init_if]
253
254[: `template<class... Args> explicit optional<T>::optional( in_place_init_if_t, bool condition, Args&&... ars );`]
255
256* [*Requires:] `is_constructible_v<T, Args&&...>` is `true`.
257* [*Effect:] If `condition` is `true`, initializes the contained value as if direct-non-list-initializing an object of type `T` with the arguments `std::forward<Args>(args)...`.
258* [*Postconditions:] `bool(*this) == condition`.
259* [*Throws:] Any exception thrown by the selected constructor of `T`.
260* [*Notes: ] `T` need not be __MOVE_CONSTRUCTIBLE__. On compilers that do not suppor variadic templates or rvalue references, this constuctor is available in limited functionality. For details [link optional_emplace_workaround see here].
261
262* [*Example:]
263``
264optional<std::vector<std::string>> ov1 {in_place_init_if, false, 3, "A"};
265assert (!ov1);
266
267optional<std::vector<std::string>> ov2 {in_place_init_if, true, 3, "A"};
268assert (ov2);
269assert (ov2->size() == 3);
270``
271
272__SPACE__
273
274[#reference_optional_constructor_factory]
275
276[: `template<InPlaceFactory> explicit optional<T>::optional( InPlaceFactory const& f );`]
277[: `template<TypedInPlaceFactory> explicit optional<T>::optional( TypedInPlaceFactory const& f );`]
278
279* [*Effect:] Constructs an `optional` with a value of `T` obtained from the
280factory.
281* [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given]
282from the factory `f` (i.e., the value [_is not copied]).
283* [*Throws:] Whatever the `T` constructor called by the factory throws.
284* [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories]
285* [*Exception Safety:] Exceptions can only be thrown during the call to
286the `T` constructor used by the factory; in that case, this constructor has
287no effect.
288* [*Example:]
289``
290class C { C ( char, double, std::string ) ; } ;
291
292C v('A',123.4,"hello");
293
294optional<C> x( in_place ('A', 123.4, "hello") ); // InPlaceFactory used
295optional<C> y( in_place<C>('A', 123.4, "hello") ); // TypedInPlaceFactory used
296
297assert ( *x == v ) ;
298assert ( *y == v ) ;
299``
300
301__SPACE__
302
303[#reference_optional_operator_equal_none_t]
304
305[: `optional& optional<T>::operator= ( none_t ) noexcept;`]
306
307* [*Effect:] If `*this` is initialized destroys its contained value.
308* [*Postconditions: ] `*this` is uninitialized.
309
310__SPACE__
311
312[#reference_optional_operator_equal_value]
313
314[: `optional& optional<T>::operator= ( T const& rhs ) ;`]
315
316* [*Effect:] Assigns the value `rhs` to an `optional`.
317* [*Postconditions: ] `*this` is initialized and its value is a ['copy] of `rhs`.
318* [*Throws:] Whatever `T::operator=( T const& )` or `T::T(T const&)` throws.
319* [*Notes:] If `*this` was initialized, `T`'s assignment operator is used,
320otherwise, its copy-constructor is used.
321* [*Exception Safety:] In the event of an exception, the initialization
322state of `*this` is unchanged and its value unspecified as far as `optional`
323is concerned (it is up to `T`'s `operator=()`). If `*this` is initially
324uninitialized and `T`'s ['copy constructor] fails, `*this` is left properly
325uninitialized.
326* [*Example:]
327``
328T x;
329optional<T> def ;
330optional<T> opt(x) ;
331
332T y;
333def = y ;
334assert ( *def == y ) ;
335opt = y ;
336assert ( *opt == y ) ;
337``
338
339
340__SPACE__
341
342[#reference_optional_operator_move_equal_value]
343
344[: `optional& optional<T>::operator= ( T&& rhs ) ;`]
345
346* [*Effect:] Moves the value `rhs` to an `optional`.
347* [*Postconditions: ] `*this` is initialized and its value is moved from `rhs`.
348* [*Throws:] Whatever `T::operator=( T&& )` or `T::T(T &&)` throws.
349* [*Notes:] If `*this` was initialized, `T`'s move-assignment operator is used,
350otherwise, its move-constructor is used.
351* [*Exception Safety:] In the event of an exception, the initialization
352state of `*this` is unchanged and its value unspecified as far as `optional`
353is concerned (it is up to `T`'s `operator=()`). If `*this` is initially
354uninitialized and `T`'s ['move constructor] fails, `*this` is left properly
355uninitialized.
356* [*Example:]
357``
358T x;
359optional<T> def ;
360optional<T> opt(x) ;
361
362T y1, y2, yR;
363def = std::move(y1) ;
364assert ( *def == yR ) ;
365opt = std::move(y2) ;
366assert ( *opt == yR ) ;
367``
368
369
370__SPACE__
371
372[#reference_optional_operator_equal_optional]
373
374[: `optional& optional<T>::operator= ( optional const& rhs ) ;`]
375
376* [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `CopyAssignable`.
377* [*Effects:]
378[table
379 []
380 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
381 [[[*`rhs` contains a value]][assigns `*rhs` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `*rhs`]]
382 [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
383]
384* [*Returns:] `*this`;
385* [*Postconditions:] `bool(rhs) == bool(*this)`.
386* [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged.
387If an exception is thrown during the call to `T`'s copy constructor, no effect.
388If an exception is thrown during the call to `T`'s copy assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
389* [*Example:]
390``
391T v;
392optional<T> opt(v);
393optional<T> def ;
394
395opt = def ;
396assert ( !def ) ;
397// previous value (copy of 'v') destroyed from within 'opt'.
398``
399
400
401__SPACE__
402
403[#reference_optional_operator_move_equal_optional]
404
405[: `optional& optional<T>::operator= ( optional&& rhs ) noexcept(`['see below]`);`]
406
407* [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `MoveAssignable`.
408* [*Effects:]
409[table
410 []
411 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
412 [[[*`rhs` contains a value]][assigns `std::move(*rhs)` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `std::move(*rhs)`]]
413 [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
414]
415* [*Returns:] `*this`;
416* [*Postconditions:] `bool(rhs) == bool(*this)`.
417* [*Remarks:] The expression inside `noexcept` is equivalent to `is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value`.
418* [*Exception Safety:] If any exception is thrown, the initialization state of `*this` and `rhs` remains unchanged. If an exception is
419thrown during the call to `T`'s move constructor, the state of `*rhs` is determined by the exception safety guarantee
420of `T`'s move constructor. If an exception is thrown during the call to T's move-assignment, the state of `**this` and `*rhs` is determined by the exception safety guarantee of T's move assignment.
421* [*Example:]
422``
423optional<T> opt(T(2)) ;
424optional<T> def ;
425
426opt = def ;
427assert ( def ) ;
428assert ( opt ) ;
429assert ( *opt == T(2) ) ;
430``
431
432
433__SPACE__
434
435
436[#reference_optional_operator_equal_other_optional]
437
438[: `template<U> optional& optional<T>::operator= ( optional<U> const& rhs ) ;`]
439
440* [*Effect:]
441[table
442 []
443 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
444 [[[*`rhs` contains a value]][assigns `*rhs` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `*rhs`]]
445 [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
446]
447* [*Returns:] `*this`.
448* [*Postconditions:] `bool(rhs) == bool(*this)`.
449* [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged.
450If an exception is thrown during the call to `T`'s constructor, no effect.
451If an exception is thrown during the call to `T`'s assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
452* [*Example:]
453``
454T v;
455optional<T> opt0(v);
456optional<U> opt1;
457
458opt1 = opt0 ;
459assert ( *opt1 == static_cast<U>(v) ) ;
460``
461
462__SPACE__
463
464[#reference_optional_operator_move_equal_other_optional]
465
466[: `template<U> optional& optional<T>::operator= ( optional<U>&& rhs ) ;`]
467
468* [*Effect:]
469[table
470 []
471 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
472 [[[*`rhs` contains a value]][assigns `std::move(*rhs)` to the contained value][initializes the contained value as if direct-initializing an object of type `T` with `std::move(*rhs)`]]
473 [[[*`rhs` does not contain a value]][destroys the contained value by calling `val->T::~T()`][no effect]]
474]
475* [*Returns:] `*this`.
476* [*Postconditions:] `bool(rhs) == bool(*this)`.
477* [*Exception Safety:] If any exception is thrown, the result of the expression `bool(*this)` remains unchanged.
478If an exception is thrown during the call to `T`'s constructor, no effect.
479If an exception is thrown during the call to `T`'s assignment, the state of its contained value is as defined by the exception safety guarantee of `T`'s copy assignment.
480* [*Example:]
481``
482T v;
483optional<T> opt0(v);
484optional<U> opt1;
485
486opt1 = std::move(opt0) ;
487assert ( opt0 );
488assert ( opt1 )
489assert ( *opt1 == static_cast<U>(v) ) ;
490``
491
492__SPACE__
493
494[#reference_optional_emplace]
495
496[: `template<class... Args> void optional<T>::emplace( Args&&... args );`]
497
498* [*Requires:] The compiler supports rvalue references and variadic templates.
499* [*Effect:] If `*this` is initialized calls `*this = none`.
500 Then initializes in-place the contained value as if direct-initializing an object
501 of type `T` with `std::forward<Args>(args)...`.
502* [*Postconditions: ] `*this` is [_initialized].
503* [*Throws:] Whatever the selected `T`'s constructor throws.
504* [*Exception Safety:] If an exception is thrown during the initialization of `T`, `*this` is ['uninitialized].
505* [*Notes:] `T` need not be __MOVE_CONSTRUCTIBLE__ or `MoveAssignable`. On compilers that do not suppor variadic templates or rvalue references, this function is available in limited functionality. For details [link optional_emplace_workaround see here].
506* [*Example:]
507``
508T v;
509optional<const T> opt;
510opt.emplace(0); // create in-place using ctor T(int)
511opt.emplace(); // destroy previous and default-construct another T
512opt.emplace(v); // destroy and copy-construct in-place (no assignment called)
513``
514
515__SPACE__
516
517[#reference_optional_operator_equal_factory]
518
519[: `template<InPlaceFactory> optional<T>& optional<T>::operator=( InPlaceFactory const& f );`]
520[: `template<TypedInPlaceFactory> optional<T>& optional<T>::operator=( TypedInPlaceFactory const& f );`]
521
522* [*Effect:] Assigns an `optional` with a value of `T` obtained from the
523factory.
524* [*Postconditions: ] `*this` is [_initialized] and its value is ['directly given]
525from the factory `f` (i.e., the value [_is not copied]).
526* [*Throws:] Whatever the `T` constructor called by the factory throws.
527* [*Notes:] See [link boost_optional.tutorial.in_place_factories In-Place Factories]
528* [*Exception Safety:] Exceptions can only be thrown during the call to
529the `T` constructor used by the factory; in that case, the `optional` object
530will be reset to be ['uninitialized].
531
532__SPACE__
533
534[#reference_optional_reset_value]
535
536[: `void optional<T>::reset( T const& v ) ;`]
537* [*Deprecated:] same as `operator= ( T const& v) ;`
538
539__SPACE__
540
541[#reference_optional_reset]
542
543[: `void optional<T>::reset() noexcept ;`]
544* [*Deprecated:] Same as `operator=( none_t );`
545
546__SPACE__
547
548[#reference_optional_get]
549
550[: `T const& optional<T>::get() const ;`]
551[: `T& optional<T>::get() ;`]
552
553[: `inline T const& get ( optional<T> const& ) ;`]
554[: `inline T& get ( optional<T> &) ;`]
555
556* [*Requires:] `*this` is initialized
557* [*Returns:] A reference to the contained value
558* [*Throws:] Nothing.
559* [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
560
561
562__SPACE__
563
564[#reference_optional_operator_asterisk]
565
566[: `T const& optional<T>::operator*() const& ;`]
567[: `T& optional<T>::operator*() &;`]
568
569* [*Requires:] `*this` is initialized
570* [*Returns:] A reference to the contained value
571* [*Throws:] Nothing.
572* [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. On compilers that do not support ref-qualifiers on member functions these two overloads are replaced with the classical two: a `const` and non-`const` member functions.
573* [*Example:]
574``
575T v ;
576optional<T> opt ( v );
577T const& u = *opt;
578assert ( u == v ) ;
579T w ;
580*opt = w ;
581assert ( *opt == w ) ;
582``
583
584__SPACE__
585
586[#reference_optional_operator_asterisk_move]
587
588[: `T&& optional<T>::operator*() &&;`]
589
590* [*Requires:] `*this` contains a value.
591* [*Effects:] Equivalent to `return std::move(*val);`.
592* [*Notes:] The requirement is asserted via `BOOST_ASSERT()`. On compilers that do not support ref-qualifiers on member functions this overload is not present.
593
594
595__SPACE__
596
597[#reference_optional_value]
598
599[: `T const& optional<T>::value() const& ;`]
600[: `T& optional<T>::value() & ;`]
601
602* [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`.
603* [*Notes:] On compilers that do not support ref-qualifiers on member functions these two overloads are replaced with the classical two: a `const` and non-`const` member functions.
604* [*Example:]
605``
606T v ;
607optional<T> o0, o1 ( v );
608assert ( o1.value() == v );
609
610try {
611 o0.value(); // throws
612 assert ( false );
613}
614catch(bad_optional_access&) {
615 assert ( true );
616}
617``
618
619__SPACE__
620
621[#reference_optional_value_move]
622
623[: `T&& optional<T>::value() && ;`]
624
625* [*Effects:] Equivalent to `return bool(*this) ? std::move(*val) : throw bad_optional_access();`.
626* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
627
628__SPACE__
629
630
631[#reference_optional_value_or]
632
633[: `template<class U> T optional<T>::value_or(U && v) const& ;`]
634
635* [*Effects:] Equivalent to `if (*this) return **this; else return std::forward<U>(v);`.
636* [*Remarks:] If `T` is not __COPY_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed.
637* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function. On compilers without rvalue reference support the type of `v` becomes `U const&`.
638
639__SPACE__
640
641[#reference_optional_value_or_move]
642
643[: `template<class U> T optional<T>::value_or(U && v) && ;`]
644
645* [*Effects:] Equivalent to `if (*this) return std::move(**this); else return std::forward<U>(v);`.
646* [*Remarks:] If `T` is not __MOVE_CONSTRUCTIBLE__ or `U &&` is not convertible to `T`, the program is ill-formed.
647* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
648
649__SPACE__
650
651[#reference_optional_value_or_call]
652
653[: `template<class F> T optional<T>::value_or_eval(F f) const& ;`]
654
655* [*Requires:] `T` is __COPY_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
656* [*Effects:] `if (*this) return **this; else return f();`.
657* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is replaced with the `const`-qualified member function.
658* [*Example:]
659``
660int complain_and_0()
661{
662 clog << "no value returned, using default" << endl;
663 return 0;
664}
665
666optional<int> o1 = 1;
667optional<int> oN = none;
668
669int i = o1.value_or_eval(complain_and_0); // fun not called
670assert (i == 1);
671
672int j = oN.value_or_eval(complain_and_0); // fun called
673assert (i == 0);
674``
675
676__SPACE__
677
678[#reference_optional_value_or_call_move]
679
680[: `template<class F> T optional<T>::value_or_eval(F f) && ;`]
681
682* [*Requires:] `T` is __MOVE_CONSTRUCTIBLE__ and `F` models a __SGI_GENERATOR__ whose result type is convertible to `T`.
683* [*Effects:] `if (*this) return std::move(**this); else return f();`.
684* [*Notes:] On compilers that do not support ref-qualifiers on member functions this overload is not present.
685
686__SPACE__
687
688[#reference_optional_get_value_or_value]
689
690[: `T const& optional<T>::get_value_or( T const& default) const ;`]
691[: `T& optional<T>::get_value_or( T& default ) ;`]
692
693* [*Deprecated:] Use `value_or()` instead.
694* [*Returns:] A reference to the contained value, if any, or `default`.
695* [*Throws:] Nothing.
696* [*Example:]
697``
698T v, z ;
699optional<T> def;
700T const& y = def.get_value_or(z);
701assert ( y == z ) ;
702
703optional<T> opt ( v );
704T const& u = opt.get_value_or(z);
705assert ( u == v ) ;
706assert ( u != z ) ;
707``
708
709
710__SPACE__
711
712[#reference_optional_get_ptr]
713
714[: `T const* optional<T>::get_ptr() const ;`]
715[: `T* optional<T>::get_ptr() ;`]
716
717* [*Returns:] If `*this` is initialized, a pointer to the contained value;
718else `0` (['null]).
719* [*Throws:] Nothing.
720* [*Notes:] The contained value is permanently stored within `*this`, so you
721should not hold nor delete this pointer
722* [*Example:]
723``
724T v;
725optional<T> opt(v);
726optional<T> const copt(v);
727T* p = opt.get_ptr() ;
728T const* cp = copt.get_ptr();
729assert ( p == get_pointer(opt) );
730assert ( cp == get_pointer(copt) ) ;
731``
732
733__SPACE__
734
735[#reference_optional_operator_arrow]
736
737[: `T const* optional<T>::operator ->() const ;`]
738[: `T* optional<T>::operator ->() ;`]
739
740* [*Requires: ] `*this` is initialized.
741* [*Returns:] A pointer to the contained value.
742* [*Throws:] Nothing.
743* [*Notes:] The requirement is asserted via `BOOST_ASSERT()`.
744* [*Example:]
745``
746struct X { int mdata ; } ;
747X x ;
748optional<X> opt (x);
749opt->mdata = 2 ;
750``
751
752__SPACE__
753
754[#reference_optional_operator_bool]
755
756[: `explicit optional<T>::operator bool() const noexcept ;`]
757
758* [*Returns:] `get_ptr() != 0`.
759* [*Notes:] On compilers that do not support explicit conversion operators this falls back to safe-bool idiom.
760* [*Example:]
761``
762optional<T> def ;
763assert ( def == 0 );
764optional<T> opt ( v ) ;
765assert ( opt );
766assert ( opt != 0 );
767``
768
769__SPACE__
770
771[#reference_optional_operator_not]
772
773[: `bool optional<T>::operator!() noexcept ;`]
774
775* [*Returns:] If `*this` is uninitialized, `true`; else `false`.
776* [*Notes:] This operator is provided for those compilers which can't
777use the ['unspecified-bool-type operator] in certain boolean contexts.
778* [*Example:]
779``
780optional<T> opt ;
781assert ( !opt );
782*opt = some_T ;
783
784// Notice the "double-bang" idiom here.
785assert ( !!opt ) ;
786``
787
788__SPACE__
789
790[#reference_optional_is_initialized]
791
792[: `bool optional<T>::is_initialized() const ;`]
793
794* [*Deprecated:] Same as `explicit operator bool () ;`
795
796
797[endsect]
798
799[section Detailed Semantics - Optional References]
800
801__SPACE__
802
803[#reference_optional_ref_default_ctor]
804
805[: `optional<T&>::optional() noexcept;`]
806[: `optional<T&>::optional(none_t) noexcept;`]
807
808* [*Postconditions:] `bool(*this) == false`; `*this` refers to nothing.
809
810
811__SPACE__
812
813[#reference_optional_ref_value_ctor]
814
815[: `template<class R> optional<T&>::optional(R&& r) noexcept;`]
816* [*Postconditions:] `bool(*this) == true`; `addressof(**this) == addressof(r)`.
817* [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
818* [*Notes:] This constructor is declared `explicit` on compilers that do not correctly suport binding to const lvalues of integral types. For more details [link optional_reference_binding see here].
819* [*Example:]
820``
821T v;
822T& vref = v ;
823optional<T&> opt(vref);
824assert ( *opt == v ) ;
825++ v ; // mutate referee
826assert (*opt == v);
827``
828
829__SPACE__
830
831[#reference_optional_ref_cond_value_ctor]
832
833[: `template<class R> optional<T&>::optional(bool cond, R&& r) noexcept;`]
834* [*Effects: ] Initializes `ref` with expression `cond ? addressof(r) : nullptr`.
835* [*Postconditions:] `bool(*this) == cond`; If `bool(*this)`, `addressof(**this) == addressof(r)`.
836* [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
837
838__SPACE__
839
840[#reference_optional_ref_copy_ctor]
841
842[: `optional<T&>::optional ( optional const& rhs ) noexcept ;`]
843
844* [*Effects: ] Initializes `ref` with expression `rhs.ref`.
845
846* [*Postconditions:] `bool(*this) == bool(rhs)`.
847
848* [*Example:]
849``
850optional<T&> uninit ;
851assert (!uninit);
852
853optional<T&> uinit2 ( uninit ) ;
854assert ( uninit2 == uninit );
855
856T v = 2 ; T& ref = v ;
857optional<T> init(ref);
858assert ( *init == v ) ;
859
860optional<T> init2 ( init ) ;
861assert ( *init2 == v ) ;
862
863v = 3 ;
864
865assert ( *init == 3 ) ;
866assert ( *init2 == 3 ) ;
867``
868
869__SPACE__
870
871[#reference_optional_ref_ctor_from_opt_U]
872
873[: `template<class U> explicit optional<T&>::optional ( optional<U&> const& rhs ) noexcept ;`]
874
875* [*Requires:] `is_convertible<U&, T&>::value` is `true`.
876
877* [*Effects: ] Initializes `ref` with expression `rhs.ref`.
878
879* [*Postconditions:] `bool(*this) == bool(rhs)`.
880
881
882__SPACE__
883
884[#reference_optional_ref_assign_none_t]
885
886[: `optional<T&>::operator= ( none_t ) noexcept ;`]
887
888* [*Effects: ] Assigns `ref` with expression `nullptr`.
889
890* [*returns:] `*this`.
891
892* [*Postconditions:] `bool(*this) == false`.
893
894
895
896[#reference_optional_ref_copy_assign]
897
898[: `optional& optional<T&>::operator= ( optional const& rhs ) noexcept ;`]
899
900* [*Effects: ] Assigns `ref` with expression `rhs.ref`.
901
902* [*returns:] `*this`.
903
904* [*Postconditions:] `bool(*this) == bool(rhs)`.
905
906* [*Notes:] This behaviour is called ['rebinding semantics]. See [link boost_optional.tutorial.optional_references.rebinding_semantics_for_assignment_of_optional_references here] for details.
907
908* [*Example:]
909``
910int a = 1 ;
911int b = 2 ;
912T& ra = a ;
913T& rb = b ;
914optional<int&> def ;
915optional<int&> ora(ra) ;
916optional<int&> orb(rb) ;
917
918def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb'
919assert ( *def == b ) ;
920*def = ora ; // changes the value of 'b' to a copy of the value of 'a'
921assert ( b == a ) ;
922int c = 3;
923int& rc = c ;
924optional<int&> orc(rc) ;
925ora = orc ; // REBINDS ora to 'c' through 'rc'
926c = 4 ;
927assert ( *ora == 4 ) ;
928``
929
930
931[#reference_optional_ref_assign_optional_U]
932
933[: `template<class U> optional& optional<T&>::operator= ( optional<U&> const& rhs ) noexcept ;`]
934
935* [*Requires:] `is_convertible<U&, T&>::value` is `true`.
936
937* [*Effects: ] Assigns `ref` with expression `rhs.ref`.
938
939* [*returns:] `*this`.
940
941* [*Postconditions:] `bool(*this) == bool(rhs)`.
942
943
944__SPACE__
945
946[#reference_optional_ref_assign_R]
947
948[: `template<class R> optional& optional<T&>::operator= ( R&& r ) noexcept ;`]
949
950* [*Effects: ] Assigns `ref` with expression `r`.
951
952* [*returns:] `*this`.
953
954* [*Postconditions:] `bool(*this) == true`.
955
956* [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
957
958* [*Example:]
959``
960int a = 1 ;
961int b = 2 ;
962T& ra = a ;
963T& rb = b ;
964optional<int&> def ;
965optional<int&> opt(ra) ;
966
967def = rb ; // binds 'def' to 'b' through 'rb'
968assert ( *def == b ) ;
969*def = a ; // changes the value of 'b' to a copy of the value of 'a'
970assert ( b == a ) ;
971int c = 3;
972int& rc = c ;
973opt = rc ; // REBINDS to 'c' through 'rc'
974c = 4 ;
975assert ( *opt == 4 ) ;
976``
977
978__SPACE__
979
980[#reference_optional_ref_emplace_R]
981
982[: `void optional<T&>::emplace( R&& r ) noexcept ;`]
983* [*Effects: ] Assigns `ref` with expression `r`.
984* [*Postconditions:] `bool(*this) == true`.
985* [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
986
987__SPACE__
988
989[#reference_optional_ref_get]
990[: `T& optional<T&>::get() const ;`]
991[: `T& optional<T&>::operator *() const ;`]
992* [*Requires:] `bool(*this) == true`.
993* [*Effects: ] Returns `*ref`.
994* [*Throws: ] Nothing.
995* [*Example:]
996``
997T v ;
998T& vref = v ;
999optional<T&> opt ( vref );
1000T const& vref2 = *opt;
1001assert ( vref2 == v ) ;
1002++ v ;
1003assert ( *opt == v ) ;
1004``
1005
1006__SPACE__
1007
1008[#reference_optional_ref_arrow]
1009[: `T* optional<T&>::operator -> () const ;`]
1010* [*Requires:] `bool(*this) == true`.
1011* [*Effects: ] Returns `ref`.
1012* [*Throws: ] Nothing.
1013
1014__SPACE__
1015
1016[#reference_optional_ref_value]
1017[: `T& optional<T&>::value() const ;`]
1018* [*Effects:] Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`.
1019
1020__SPACE__
1021
1022[#reference_optional_ref_value_or]
1023[: `template<class R> T& optional<T&>::value_or( R&& r ) const noexcept;`]
1024* [*Effects:] Equivalent to `if (*this) return **this; else return r;`.
1025* [*Remarks:] Unless `R` is an lvalue reference, the program is ill-formed.
1026
1027__SPACE__
1028
1029[#reference_optional_ref_value_or_eval]
1030[: `template<class F> T& optional<T&>::value_or( F f ) const ;`]
1031* [*Effects:] Equivalent to `if (*this) return **this; else return f();`.
1032* [*Remarks:] Unless `decltype(f())` is an lvalue reference, the program is ill-formed.
1033
1034__SPACE__
1035
1036[#reference_optional_ref_get_ptr]
1037[: `T* optional<T&>::get_ptr () const noexcept;`]
1038* [*Returns:] `ref`.
1039
1040__SPACE__
1041
1042[#reference_optional_ref_operator_bool]
1043[: `optional<T&>::operator bool () const noexcept;`]
1044* [*Returns:] `bool(ref)`.
1045
1046__SPACE__
1047
1048[#reference_optional_ref_operator_not]
1049[: `optional<T&>::operator ! () const noexcept;`]
1050* [*Returns:] `!bool(ref)`.
1051
1052__SPACE__
1053
1054[#reference_optional_ref_reset]
1055[: `void optional<T&>::reset() noexcept;`]
1056* [*Effects:] Use `*this = none` instead.
1057* [*Remarks:] This function is depprecated.
1058
1059__SPACE__
1060
1061[#reference_optional_ref_reset_value]
1062[: `template<class R> void optional<T&>::reset ( R&& r) noexcept;`]
1063* [*Effects:] Equivalent to `*this = std::forward<R>(r)`.
1064* [*Remarks:] This function is depprecated.
1065
1066__SPACE__
1067
1068[#reference_optional_ref_is_initialized]
1069[: `bool optional<T&>::is_initialized() const noexcept;`]
1070* [*Effects:] Equivalent to `return bool(*this)`.
1071* [*Remarks:] This function is depprecated.
1072
1073__SPACE__
1074
1075[#reference_optional_ref_get_value_or_value]
1076[: `template<class R> T& optional<T&>::get_value_or( R&& r ) const noexcept;`]
1077* [*Effects:] Equivalent to `return value_or(std::forward<R>(r);`.
1078* [*Remarks:] This function is depprecated.
1079
1080[endsect]
1081
1082
1083[section Detailed Semantics - Free Functions]
1084
1085
1086__SPACE__
1087
1088[#reference_make_optional_value]
1089
1090[: `optional<T> make_optional( T const& v )`]
1091
1092* [*Returns: ] `optional<T>(v)` for the ['deduced] type `T` of `v`.
1093* [*Example:]
1094``
1095template<class T> void foo ( optional<T> const& opt ) ;
1096
1097foo ( make_optional(1+1) ) ; // Creates an optional<int>
1098``
1099
1100__SPACE__
1101
1102[#reference_make_optional_bool_value]
1103
1104[: `optional<T> make_optional( bool condition, T const& v )`]
1105
1106* [*Returns: ] `optional<T>(condition, v)` for the ['deduced] type `T` of `v`.
1107* [*Example:]
1108``
1109optional<double> calculate_foo()
1110{
1111 double val = compute_foo();
1112 return make_optional(is_not_nan_and_finite(val),val);
1113}
1114
1115optional<double> v = calculate_foo();
1116if ( !v )
1117 error("foo wasn't computed");
1118``
1119
1120__SPACE__
1121
1122[#reference_operator_compare_equal_optional_optional]
1123
1124[: `bool operator == ( optional<T> const& x, optional<T> const& y );`]
1125
1126* [*Requires:] `T` shall meet requirements of __SGI_EQUALITY_COMPARABLE__.
1127* [*Returns:] If both `x` and `y` are initialized, `(*x == *y)`. If only
1128`x` or `y` is initialized, `false`. If both are uninitialized, `true`.
1129* [*Notes:] This definition guarantees that `optional<T>` not containing a value is compared unequal to any `optional<T>` containing any value, and equal to any other `optional<T>` not containing a value.
1130Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator==` directly in generic code which expect to be given either an `optional<T>` or a pointer; use
1131__FUNCTION_EQUAL_POINTEES__ instead
1132* [*Example:]
1133``
1134optional<T> oN, oN_;
1135optional<T> o1(T(1)), o1_(T(1));
1136optional<T> o2(T(2));
1137
1138assert ( oN == oN ); // Identity implies equality
1139assert ( o1 == o1 ); //
1140
1141assert ( oN == oN_ ); // Both uninitialized compare equal
1142
1143assert ( oN != o1 ); // Initialized unequal to initialized.
1144
1145assert ( o1 == o1_ ); // Both initialized compare as (*lhs == *rhs)
1146assert ( o1 != o2 ); //
1147``
1148
1149__SPACE__
1150
1151[#reference_operator_compare_less_optional_optional]
1152
1153[: `bool operator < ( optional<T> const& x, optional<T> const& y );`]
1154
1155* [*Requires:] Expression `*x < *y` shall be well-formed and its result shall be convertible to `bool`.
1156* [*Returns:] `(!y) ? false : (!x) ? true : *x < *y`.
1157* [*Notes:] This definition guarantees that `optional<T>` not containing a value is ordered as less than any `optional<T>` containing any value, and equivalent to any other `optional<T>` not containing a value.
1158Pointers have shallow relational operators while `optional` has deep relational operators. Do not use `operator<` directly in generic code
1159which expect to be given either an `optional<T>` or a pointer; use __FUNCTION_LESS_POINTEES__ instead. `T` need not be __SGI_LESS_THAN_COMPARABLE__. Only single `operator<` is required. Other relational operations are defined in terms of this one. If `T`'s `operator<` satisfies the axioms of __SGI_LESS_THAN_COMPARABLE__ (transitivity, antisymmetry and irreflexivity), `optinal<T>` is __SGI_LESS_THAN_COMPARABLE__.
1160* [*Example:]
1161``
1162optional<T> oN, oN_;
1163optional<T> o0(T(0));
1164optional<T> o1(T(1));
1165
1166assert ( !(oN < oN) ); // Identity implies equivalence
1167assert ( !(o1 < o1) );
1168
1169assert ( !(oN < oN_) ); // Two uninitialized are equivalent
1170assert ( !(oN_ < oN) );
1171
1172assert ( oN < o0 ); // Uninitialized is less than initialized
1173assert ( !(o0 < oN) );
1174
1175assert ( o1 < o2 ) ; // Two initialized compare as (*lhs < *rhs)
1176assert ( !(o2 < o1) ) ;
1177assert ( !(o2 < o2) ) ;
1178``
1179
1180__SPACE__
1181
1182[#reference_operator_compare_not_equal_optional_optional]
1183
1184[: `bool operator != ( optional<T> const& x, optional<T> const& y );`]
1185
1186* [*Returns: ] `!( x == y );`
1187
1188__SPACE__
1189
1190[#reference_operator_compare_greater_optional_optional]
1191
1192[: `bool operator > ( optional<T> const& x, optional<T> const& y );`]
1193
1194* [*Returns: ] `( y < x );`
1195
1196__SPACE__
1197
1198[#reference_operator_compare_less_or_equal_optional_optional]
1199
1200[: `bool operator <= ( optional<T> const& x, optional<T> const& y );`]
1201
1202* [*Returns: ] `!( y < x );`
1203
1204__SPACE__
1205
1206[#reference_operator_compare_greater_or_equal_optional_optional]
1207
1208[: `bool operator >= ( optional<T> const& x, optional<T> const& y );`]
1209
1210* [*Returns: ] `!( x < y );`
1211
1212__SPACE__
1213
1214[#reference_operator_compare_equal_optional_none]
1215
1216[: `bool operator == ( optional<T> const& x, none_t ) noexcept;`]
1217[: `bool operator == ( none_t, optional<T> const& x ) noexcept;`]
1218
1219* [*Returns:] `!x`.
1220* [*Notes:] `T` need not meet requirements of __SGI_EQUALITY_COMPARABLE__.
1221
1222
1223__SPACE__
1224
1225[#reference_operator_compare_not_equal_optional_none]
1226
1227[: `bool operator != ( optional<T> const& x, none_t ) noexcept;`]
1228[: `bool operator != ( none_t, optional<T> const& x ) noexcept;`]
1229
1230* [*Returns: ] `bool(x);`
1231
1232
1233__SPACE__
1234
1235
1236[#reference_free_get_pointer]
1237[: `auto get_pointer ( optional<T>& o ) -> typename optional<T>::pointer_type ;`]
1238[: `auto get_pointer ( optional<T> const& o ) -> typename optional<T>::pointer_const_type ;`]
1239* [*Returns:] `o.get_ptr()`.
1240* [*Throws:] Nothing.
1241
1242__SPACE__
1243
1244
1245[#reference_free_get_value_or]
1246[: `auto get_optional_value_or ( optional<T>& o, typename optional<T>::reference_type def ) -> typename optional<T>::reference_type ;`]
1247[: `auto get_optional_value_or ( optional<T> const& o, typename optional<T>::reference_const_type def ) -> typename optional<T>::reference_const_type ;`]
1248* [*Returns:] `o.get_value_or(def)`.
1249* [*Throws:] Nothing.
1250* [*Remarks:] This function is deprecated.
1251
1252__SPACE__
1253
1254[#reference_swap_optional_optional]
1255
1256[: `void swap ( optional<T>& x, optional<T>& y ) ;`]
1257
1258* [*Requires:] Lvalues of type `T` shall be swappable and `T` shall be __MOVE_CONSTRUCTIBLE__.
1259* [*Effects:]
1260[table
1261 []
1262 [[][[*`*this` contains a value]][[*`*this` does not contain a value]]]
1263 [[[*`rhs` contains a value]][calls `swap(*(*this), *rhs)`][initializes the contained value of `*this` as if direct-initializing an object of type `T` with the expression `std::move(*rhs)`, followed by `rhs.val->T::~T()`, `*this` contains a value and `rhs` does not contain a value]]
1264 [[[*`rhs` does not contain a value]][initializes the contained value of `rhs` as if direct-initializing an object of type `T` with the expression `std::move(*(*this))`, followed by `val->T::~T()`, `*this` does not contain a value and `rhs` contains a value][no effect]]
1265]
1266* [*Postconditions:] The states of `x` and `y` interchanged.
1267* [*Throws:] If both are initialized, whatever `swap(T&,T&)` throws. If only
1268one is initialized, whatever `T::T ( T&& )` throws.
1269* [*Example:]
1270``
1271T x(12);
1272T y(21);
1273optional<T> def0 ;
1274optional<T> def1 ;
1275optional<T> optX(x);
1276optional<T> optY(y);
1277
1278boost::swap(def0,def1); // no-op
1279
1280boost::swap(def0,optX);
1281assert ( *def0 == x );
1282assert ( !optX );
1283
1284boost::swap(def0,optX); // Get back to original values
1285
1286boost::swap(optX,optY);
1287assert ( *optX == y );
1288assert ( *optY == x );
1289``
1290
1291__SPACE__
1292
1293[#reference_swap_optional_reference]
1294[: `void swap ( optional<T&>& x, optional<T&>& y ) noexcept ;`]
1295
1296* [*Postconditions:] `x` refers to what `y` refererred to before the swap (if anything). `y` refers to whatever `x` referred to before the swap.
1297
1298* [*Example:]
1299``
1300T x(12);
1301T y(21);
1302
1303optional<T&> opt0;
1304optional<T&> optX (x);
1305optional<T&> optY (y);
1306
1307boost::swap(optX, optY);
1308assert (addressof(*optX) == addressof(y));
1309assert (addressof(*optY) == addressof(x));
1310
1311boost::swap(opt0, optX);
1312assert ( opt0 );
1313assert ( !optX );
1314assert (addressof(*opt0) == addressof(y));
1315``
1316
1317[endsect]