std::experi...al::optional(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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. πŸŽ€

  1. Constructs the object that does not contain a value. (empty vibe, no notes)
  2. Copy constructor: If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression *other. If other does not contain a value, constructs an object that does not contain a value.
  3. Move constructor: If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other) and does not make other empty: a moved-from optional still contains a value, but the value itself is moved from. If other does not contain a value, constructs an object that does not contain a value. This one is lowkey important, bestie.
  4. Constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression value. This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr.
  5. Constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(value). This constructor is constexpr if the constructor of T selected by direct-initialization is constexpr.
  6. Constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T from the arguments std::forward<Args>(args)....
  7. Constructs an optional object that contains a value, initialized as if direct-initializing (but not direct-list-initializing) an object of type T from the arguments ilist, std::forward<Args>(args).... The function does not participate in overload resolution if std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value != true. that != true is doing a whole lot, okay

🌸 Parameters 🌸

  • other β€” another optional object 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 πŸ¦‹

  • T must meet the requirements of CopyConstructible in order to use overloads (2,4).
  • T must meet the requirements of MoveConstructible in order to use overloads (3,5).

πŸ”₯ Exceptions πŸ”₯

  • (2) β€” Throws any exception thrown by the constructor of T.
  • (3) β€” Throws any exception thrown by the constructor of T. Has the following noexcept declaration:
noexcept(std::is_nothrow_move_constructible<T>::value)
  • (4-7) β€” Throws any exception thrown by the constructor of T.

πŸŽ€ 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 an optional object (function template).

🌈 Categories 🌈

  • Noindexed pages
  • conditionally noexcept

mwah πŸ’‹ β€” source/version http://cppreference.com, dated 2024.06.10, page std::experi...al::optional(3). bye bestie 🫢

πŸ“¦ where this page comes from