Figment
A zero-overhead std::optional alternative
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
fgmnt::figment< T > Union Template Reference

A figment holds either an object or nothing. More...

Public Types

using value_type = T
 Underlying object type.
 

Public Member Functions

constexpr figment ()
 Does nothing. You have to initialize the underlying object manually.
 
constexpr ~figment ()
 Does nothing. You have to destroy the underlying object manually.
 
template<typename... Args>
constexpr figment (Args &&...args)
 Starts the lifetime of the underlying object. More...
 
template<typename U >
constexpr figment (std::initializer_list< U > list)
 Starts the lifetime of the underlying object. More...
 
template<typename... Args>
void create (Args &&...args)
 Starts the lifetime of the underlying object. More...
 
template<typename U >
void create (std::initializer_list< U > list)
 Starts the lifetime of the underlying object. More...
 
constexpr void destroy ()
 Destroys the underlying object, ending its lifetime.
 
template<typename... Args>
constexpr void replace (Args &&...args)
 Replaces the underlying object. More...
 
template<typename U >
constexpr void replace (std::initializer_list< U > list)
 Replaces the underlying object. More...
 
constexpr T & to_underlying ()
 Accessor to the underlying object.
 
constexpr const T & to_underlying () const
 Accessor to the underlying object, as if it were of type std::add_const_t<T>.
 
constexpr decltype(auto) begin ()
 Same as calling std::begin with the underlying object.
 
constexpr decltype(auto) begin () const
 Same as calling std::begin with the underlying object, as if it were of type std::add_const_t<T>.
 
constexpr decltype(auto) end ()
 Same as calling std::end with the underlying object.
 
constexpr decltype(auto) end () const
 Same as calling std::end with the underlying object, as if it were of type std::add_const_t<T>.
 
constexpr T * operator-> ()
 Accesses the members of the underlying object.
 
constexpr const T * operator-> () const
 Accesses the members of the underlying object, as if it were of type std::add_const_t<T>.
 
constexpr decltype(auto) operator& ()
 Same as using the & operator in the underlying object.
 
constexpr decltype(auto) operator& () const
 Same as using the & operator in the underlying object, as if it were of type std::add_const_t<T>.
 
template<typename Arg >
constexpr decltype(auto) operator[] (Arg &&rhs)
 Same as using the [] operator in the underlying object.
 
template<typename Arg >
constexpr decltype(auto) operator[] (Arg &&rhs) const
 Same as using the [] operator in the underlying object, as if it were of type std::add_const_t<T>.
 
template<typename Arg >
constexpr decltype(auto) operator= (Arg &&rhs)
 Same as using the = operator in the underlying object.
 
template<typename Arg >
constexpr decltype(auto) operator= (Arg &&rhs) const
 Same as using the = operator in the underlying object, as if it were of type std::add_const_t<T>.
 
template<typename Arg >
constexpr decltype(auto) operator<=> (Arg &&rhs)
 Same as using the <=> operator in the underlying object.
 
template<typename Arg >
constexpr decltype(auto) operator<=> (Arg &&rhs) const
 Same as using the <=> operator in the underlying object, as if it were of type std::add_const_t<T>.
 
template<typename Arg >
constexpr bool operator== (Arg &&rhs)
 Same as using the == operator in the underlying object.
 
template<typename Arg >
constexpr bool operator== (Arg &&rhs) const
 Same as using the == operator in the underlying object, as if it were of type std::add_const_t<T>.
 
constexpr operator T& ()
 Returns a reference to the underlying object.
 
constexpr operator const T & () const
 Returns a const reference to the underlying object.
 

Detailed Description

template<typename T>
requires (!std::is_reference_v<T> && !std::is_volatile_v<T>)
union fgmnt::figment< T >

A figment holds either an object or nothing.

Holds no information whatsoever about whether or not an object is currently stored. Only use this class if the accessor code is unreachable under unsafe conditions. Managing the underlying objects' lifetime is your responsibility.

If you need a boolean flag to track the state of a figment, this isn't for you. Use an std::optional instead.

There's not much point in using a const figment. You can use a const T, or temporarily call T's const member functions using std::as_const on the figment.

Template Parameters
TType of the underlying object

Constructor & Destructor Documentation

◆ figment() [1/2]

template<typename T >
template<typename... Args>
constexpr fgmnt::figment< T >::figment ( Args &&...  args)
inlineconstexpr

Starts the lifetime of the underlying object.

Parameters
argsArguments to initialize the object with, as if by calling T's constructor.

◆ figment() [2/2]

template<typename T >
template<typename U >
constexpr fgmnt::figment< T >::figment ( std::initializer_list< U >  list)
inlineexplicitconstexpr

Starts the lifetime of the underlying object.

Parameters
listInitializer list to initialize the object with, as if by calling the constructor T::T(std::initializer_list).

Member Function Documentation

◆ create() [1/2]

template<typename T >
template<typename... Args>
void fgmnt::figment< T >::create ( Args &&...  args)
inline

Starts the lifetime of the underlying object.

The behavior is undefined if an object already exists. End its lifetime with destroy().

Parameters
argsArguments to initialize the object with, as if by calling T's constructor.

◆ create() [2/2]

template<typename T >
template<typename U >
void fgmnt::figment< T >::create ( std::initializer_list< U >  list)
inline

Starts the lifetime of the underlying object.

The behavior is undefined if an object already exists. End its lifetime with destroy().

Parameters
listInitializer list to initialize the object with, as if by calling the constructor T::T(std::initializer_list).

◆ replace() [1/2]

template<typename T >
template<typename... Args>
constexpr void fgmnt::figment< T >::replace ( Args &&...  args)
inlineconstexpr

Replaces the underlying object.

Calls T's destructor on the object, and creates another object as if using create().

The behavior is undefined if there's no underlying object

Parameters
argsArguments to initialize the new object with, as if by calling T's constructor.

◆ replace() [2/2]

template<typename T >
template<typename U >
constexpr void fgmnt::figment< T >::replace ( std::initializer_list< U >  list)
inlineconstexpr

Replaces the underlying object.

Calls T's destructor on the object, and creates another object as if using create().

The behavior is undefined if there's no underlying object

Parameters
listInitializer list to initialize the new object with, as if by calling the constructor T::T(std::initializer_list).