std::experi...al::optional(3)
π std::experi...al::optional(3) π
okay bestie, welcome to the constructor page for std::experimental::optional::optional, from the C++ Standard Libary (the header literally spells it βLibaryβ and we are keeping that detail because facts are sacred π). This is all about making an optional that either holds a value or is empty, and the overloads are giving βpick your fighterβ energy. β¨
π NAME π
std::experimental::optional::optional β std::experimental::optional::optional
β¨ Synopsis β¨
Every overload here is tagged (library fundamentals TS) in the source, and the overload numbers are basically the group-chat receipts:
constexpr optional() noexcept;
constexpr optional( std::experimental::nullopt_t ) noexcept; (1)
optional( const optional& other ); (2)
optional( optional&& other ) noexcept(/* see below */); (3)
constexpr optional( const T& value ); (4)
constexpr optional( T&& value ); (5)
template< class... Args >
constexpr explicit optional( std::experimental::in_place_t, Args&&... args ); (6)
template< class U, class... Args >
constexpr explicit optional( std::experimental::in_place_t, std::initializer_list<U> ilist, Args&&... args ); (7)
Constructs a new optional object. π
- Constructs the object that does not contain a value. (empty vibe, no notes)
- Copy constructor: If
othercontains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of typeTwith the expression*other. Ifotherdoes not contain a value, constructs an object that does not contain a value. - Move constructor: If
othercontains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of typeTwith the expressionstd::move(*other)and does not makeotherempty: a moved-fromoptionalstill contains a value, but the value itself is moved from. Ifotherdoes not contain a value, constructs an object that does not contain a value. This one is lowkey important, bestie. - Constructs an
optionalobject that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of typeTwith the expressionvalue. This constructor isconstexprif the constructor ofTselected by direct-initialization isconstexpr. - Constructs an
optionalobject that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of typeTwith the expressionstd::move(value). This constructor isconstexprif the constructor ofTselected by direct-initialization isconstexpr. - Constructs an
optionalobject that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of typeTfrom the argumentsstd::forward<Args>(args).... - Constructs an
optionalobject that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of typeTfrom the argumentsilist, std::forward<Args>(args).... The function does not participate in overload resolution ifstd::is_constructible<T, std::initializer_list<U>&, Args&&...>::value != true. that!= trueis doing a whole lot, okay
πΈ Parameters πΈ
otherβ anotheroptionalobject whose contained value to copy π«Άvalueβ value to initialize the contained value with β¨args...β arguments to initialize the contained value with πilistβ initializer list to initialize the contained value with π
π¦ Type requirements π¦
Tmust meet the requirements of CopyConstructible in order to use overloads(2,4).Tmust meet the requirements of MoveConstructible in order to use overloads(3,5).
π₯ Exceptions π₯
(2)β Throws any exception thrown by the constructor ofT.(3)β Throws any exception thrown by the constructor ofT. Has the followingnoexceptdeclaration:
noexcept(std::is_nothrow_move_constructible<T>::value)
(4-7)β Throws any exception thrown by the constructor ofT.
π Example π
// Run this code
#include <experimental/optional>
#include <iostream>
#include <string>
int main()
{
std::experimental::optional<int> o1, // empty
o2 = 1, // init from rvalue
o3 = o2; // copy-constructor
std::experimental::optional<std::string>
o4(std::experimental::in_place,
{'a', 'b', 'c'});
std::cout << *o2 << ' ' << *o3 << ' ' << *o4 << '\n';
}
π Output π
1 1 abc
π See also π
make_optionalβ creates anoptionalobject (function template).
π Categories π
Noindexed pagesconditionally noexcept
mwah π β source/version http://cppreference.com, dated 2024.06.10, page std::experi...al::optional(3). bye bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms