330 lines
10 KiB
Markdown
330 lines
10 KiB
Markdown
[optional.ctor]
|
||
|
||
# 22 General utilities library [[utilities]](./#utilities)
|
||
|
||
## 22.5 Optional objects [[optional]](optional#ctor)
|
||
|
||
### 22.5.3 Class template optional [[optional.optional]](optional.optional#optional.ctor)
|
||
|
||
#### 22.5.3.2 Constructors [optional.ctor]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3389)
|
||
|
||
The exposition-only variable template *converts-from-any-cvref* is used by some constructors for optional[.](#1.sentence-1)
|
||
|
||
template<class T, class W>constexpr bool *converts-from-any-cvref* = // *exposition only* disjunction_v<is_constructible<T, W&>, is_convertible<W&, T>,
|
||
is_constructible<T, W>, is_convertible<W, T>,
|
||
is_constructible<T, const W&>, is_convertible<const W&, T>,
|
||
is_constructible<T, const W>, is_convertible<const W, T>>;
|
||
|
||
[ð](#lib:optional,constructor)
|
||
|
||
`constexpr optional() noexcept;
|
||
constexpr optional(nullopt_t) noexcept;
|
||
`
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3408)
|
||
|
||
*Postconditions*: *this does not contain a value[.](#2.sentence-1)
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3412)
|
||
|
||
*Remarks*: No contained value is initialized[.](#3.sentence-1)
|
||
|
||
For every object type T these constructors are constexpr constructors ([[dcl.constexpr]](dcl.constexpr "9.2.6 The constexpr and consteval specifiers"))[.](#3.sentence-2)
|
||
|
||
[ð](#lib:optional,constructor_)
|
||
|
||
`constexpr optional(const optional& rhs);
|
||
`
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3424)
|
||
|
||
*Effects*: If rhs contains a value, direct-non-list-initializes the contained value
|
||
with *rhs[.](#4.sentence-1)
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3429)
|
||
|
||
*Postconditions*: rhs.has_value() == this->has_value()[.](#5.sentence-1)
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3433)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#6.sentence-1)
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3437)
|
||
|
||
*Remarks*: This constructor is defined as deleted unlessis_copy_constructible_v<T> is true[.](#7.sentence-1)
|
||
|
||
If is_trivially_copy_constructible_v<T> is true,
|
||
this constructor is trivial[.](#7.sentence-2)
|
||
|
||
[ð](#lib:optional,constructor__)
|
||
|
||
`constexpr optional(optional&& rhs) noexcept(see below);
|
||
`
|
||
|
||
[8](#8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3451)
|
||
|
||
*Constraints*: is_move_constructible_v<T> is true[.](#8.sentence-1)
|
||
|
||
[9](#9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3455)
|
||
|
||
*Effects*: If rhs contains a value, direct-non-list-initializes the contained value
|
||
with *std::move(rhs)[.](#9.sentence-1)
|
||
|
||
rhs.has_value() is unchanged[.](#9.sentence-2)
|
||
|
||
[10](#10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3461)
|
||
|
||
*Postconditions*: rhs.has_value() == this->has_value()[.](#10.sentence-1)
|
||
|
||
[11](#11)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3465)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#11.sentence-1)
|
||
|
||
[12](#12)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3469)
|
||
|
||
*Remarks*: The exception specification is equivalent tois_nothrow_move_constructible_v<T>[.](#12.sentence-1)
|
||
|
||
If is_trivially_move_constructible_v<T> is true,
|
||
this constructor is trivial[.](#12.sentence-2)
|
||
|
||
[ð](#lib:optional,constructor___)
|
||
|
||
`template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);
|
||
`
|
||
|
||
[13](#13)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3483)
|
||
|
||
*Constraints*: is_constructible_v<T, Args...> is true[.](#13.sentence-1)
|
||
|
||
[14](#14)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3487)
|
||
|
||
*Effects*: Direct-non-list-initializes the contained value with std::forward<Args>(args)...[.](#14.sentence-1)
|
||
|
||
[15](#15)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3491)
|
||
|
||
*Postconditions*: *this contains a value[.](#15.sentence-1)
|
||
|
||
[16](#16)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3495)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#16.sentence-1)
|
||
|
||
[17](#17)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3499)
|
||
|
||
*Remarks*: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor[.](#17.sentence-1)
|
||
|
||
[ð](#lib:optional,constructor____)
|
||
|
||
`template<class U, class... Args>
|
||
constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
|
||
`
|
||
|
||
[18](#18)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3511)
|
||
|
||
*Constraints*: is_constructible_v<T, initializer_list<U>&, Args...> is true[.](#18.sentence-1)
|
||
|
||
[19](#19)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3515)
|
||
|
||
*Effects*: Direct-non-list-initializes the contained value with il, std::forward<Args>(args)...[.](#19.sentence-1)
|
||
|
||
[20](#20)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3519)
|
||
|
||
*Postconditions*: *this contains a value[.](#20.sentence-1)
|
||
|
||
[21](#21)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3523)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#21.sentence-1)
|
||
|
||
[22](#22)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3527)
|
||
|
||
*Remarks*: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor[.](#22.sentence-1)
|
||
|
||
[ð](#lib:optional,constructor_____)
|
||
|
||
`template<class U = remove_cv_t<T>> constexpr explicit(see below) optional(U&& v);
|
||
`
|
||
|
||
[23](#23)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3538)
|
||
|
||
*Constraints*:
|
||
|
||
- [(23.1)](#23.1)
|
||
|
||
is_constructible_v<T, U> is true,
|
||
|
||
- [(23.2)](#23.2)
|
||
|
||
is_same_v<remove_cvref_t<U>, in_place_t> is false,
|
||
|
||
- [(23.3)](#23.3)
|
||
|
||
is_same_v<remove_cvref_t<U>, optional> is false, and
|
||
|
||
- [(23.4)](#23.4)
|
||
|
||
if T is cv bool,remove_cvref_t<U> is not a specialization of optional[.](#23.sentence-1)
|
||
|
||
[24](#24)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3548)
|
||
|
||
*Effects*: Direct-non-list-initializes the contained value with std::forward<U>(v)[.](#24.sentence-1)
|
||
|
||
[25](#25)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3552)
|
||
|
||
*Postconditions*: *this contains a value[.](#25.sentence-1)
|
||
|
||
[26](#26)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3556)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#26.sentence-1)
|
||
|
||
[27](#27)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3560)
|
||
|
||
*Remarks*: If T's selected constructor is a constexpr constructor,
|
||
this constructor is a constexpr constructor[.](#27.sentence-1)
|
||
|
||
The expression inside explicit is equivalent to:!is_convertible_v<U, T>
|
||
|
||
[ð](#lib:optional,constructor______)
|
||
|
||
`template<class U> constexpr explicit(see below) optional(const optional<U>& rhs);
|
||
`
|
||
|
||
[28](#28)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3576)
|
||
|
||
*Constraints*:
|
||
|
||
- [(28.1)](#28.1)
|
||
|
||
is_constructible_v<T, const U&> is true, and
|
||
|
||
- [(28.2)](#28.2)
|
||
|
||
if T is not cv bool,*converts-from-any-cvref*<T, optional<U>> is false[.](#28.sentence-1)
|
||
|
||
[29](#29)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3584)
|
||
|
||
*Effects*: If rhs contains a value,
|
||
direct-non-list-initializes the contained value with *rhs[.](#29.sentence-1)
|
||
|
||
[30](#30)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3589)
|
||
|
||
*Postconditions*: rhs.has_value() == this->has_value()[.](#30.sentence-1)
|
||
|
||
[31](#31)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3593)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#31.sentence-1)
|
||
|
||
[32](#32)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3597)
|
||
|
||
*Remarks*: The expression inside explicit is equivalent to:!is_convertible_v<const U&, T>
|
||
|
||
[ð](#lib:optional,constructor_______)
|
||
|
||
`template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);
|
||
`
|
||
|
||
[33](#33)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3611)
|
||
|
||
*Constraints*:
|
||
|
||
- [(33.1)](#33.1)
|
||
|
||
is_constructible_v<T, U> is true, and
|
||
|
||
- [(33.2)](#33.2)
|
||
|
||
if T is not cv bool,*converts-from-any-cvref*<T, optional<U>> is false[.](#33.sentence-1)
|
||
|
||
[34](#34)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3619)
|
||
|
||
*Effects*: If rhs contains a value,
|
||
direct-non-list-initializes the contained value with *std::move(rhs)[.](#34.sentence-1)
|
||
|
||
rhs.has_value() is unchanged[.](#34.sentence-2)
|
||
|
||
[35](#35)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3625)
|
||
|
||
*Postconditions*: rhs.has_value() == this->has_value()[.](#35.sentence-1)
|
||
|
||
[36](#36)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3629)
|
||
|
||
*Throws*: Any exception thrown by the selected constructor of T[.](#36.sentence-1)
|
||
|
||
[37](#37)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3633)
|
||
|
||
*Remarks*: The expression inside explicit is equivalent to:!is_convertible_v<U, T>
|