[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) templateconstexpr bool *converts-from-any-cvref* = // *exposition only* disjunction_v, is_convertible, is_constructible, is_convertible, is_constructible, is_convertible, is_constructible, is_convertible>; [🔗](#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 is true[.](#7.sentence-1) If is_trivially_copy_constructible_v 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 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[.](#12.sentence-1) If is_trivially_move_constructible_v is true, this constructor is trivial[.](#12.sentence-2) [🔗](#lib:optional,constructor___) `template 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 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)...[.](#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 constexpr explicit optional(in_place_t, initializer_list il, Args&&... args); ` [18](#18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3511) *Constraints*: is_constructible_v&, 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)...[.](#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> 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 is true, - [(23.2)](#23.2) is_same_v, in_place_t> is false, - [(23.3)](#23.3) is_same_v, optional> is false, and - [(23.4)](#23.4) if T is cv bool,remove_cvref_t 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(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 [🔗](#lib:optional,constructor______) `template constexpr explicit(see below) optional(const optional& rhs); ` [28](#28) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3576) *Constraints*: - [(28.1)](#28.1) is_constructible_v is true, and - [(28.2)](#28.2) if T is not cv bool,*converts-from-any-cvref*> 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 [🔗](#lib:optional,constructor_______) `template constexpr explicit(see below) optional(optional&& rhs); ` [33](#33) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3611) *Constraints*: - [(33.1)](#33.1) is_constructible_v is true, and - [(33.2)](#33.2) if T is not cv bool,*converts-from-any-cvref*> 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