gw
1.0.0
A bunch of small C++ utilities
|
Strong type wrapper. More...
#include <strong_type.hpp>
Public Types | |
using | value_type = T |
The type of the contained value. | |
using | tag_type = Tag |
The tag type. | |
Public Member Functions | |
template<typename... Args> requires std::constructible_from<value_type, Args...> | |
constexpr | strong_type (Args &&... args) noexcept(std::is_nothrow_constructible_v< value_type, Args... >) |
Construct the gw::strong_type object. | |
template<typename U , typename... Args> requires std::constructible_from<value_type, std::initializer_list<U>&, Args...> | |
constexpr | strong_type (std::initializer_list< U > ilist, Args &&... args) noexcept(std::is_nothrow_constructible_v< value_type, std::initializer_list< U > &, Args... >) |
constructs the gw::strong_type object | |
~strong_type () noexcept(std::is_nothrow_destructible_v< value_type >)=default | |
destroys the contained value | |
constexpr auto | operator-> () const noexcept -> const value_type * |
accesses the contained value | |
constexpr auto | operator-> () noexcept -> value_type * |
accesses the contained value | |
constexpr auto | operator* () const &noexcept -> const value_type & |
accesses the contained value | |
constexpr auto | operator* () &noexcept -> value_type & |
accesses the contained value | |
constexpr auto | operator* () const &&noexcept -> const value_type && |
accesses the contained value | |
constexpr auto | operator* () &&noexcept -> value_type && |
accesses the contained value | |
constexpr auto | value () const &noexcept -> const value_type & |
returns the contained value | |
constexpr auto | value () &noexcept -> value_type & |
returns the contained value | |
constexpr auto | value () const &&noexcept -> const value_type && |
returns the contained value | |
constexpr auto | value () &&noexcept -> value_type && |
returns the contained value | |
template<typename F > requires std::invocable<F, const value_type&> | |
constexpr auto | transform (F &&func) const &noexcept(noexcept(func(m_value))) -> strong_type |
returns a gw::strong_type containing the transformed contained value | |
template<typename F > requires std::invocable<F, value_type&> | |
constexpr auto | transform (F &&func) &noexcept(noexcept(func(m_value))) -> strong_type |
returns a gw::strong_type containing the transformed contained value | |
template<typename F > requires std::invocable<F, const value_type&&> | |
constexpr auto | transform (F &&func) const &&noexcept(noexcept(func(m_value))) -> strong_type |
returns a gw::strong_type containing the transformed contained value | |
template<typename F > requires std::invocable<F, value_type&&> | |
constexpr auto | transform (F &&func) &&noexcept(noexcept(func(m_value))) -> strong_type |
returns a gw::strong_type containing the transformed contained value | |
constexpr void | swap (strong_type &rhs) noexcept(std::is_nothrow_swappable_v< value_type >) |
specializes the std::swap algorithm | |
constexpr void | reset () noexcept(std::is_nothrow_default_constructible_v< value_type >) |
destroys any contained value | |
template<typename... Args> requires std::constructible_from<value_type, Args...> | |
constexpr auto | emplace (Args &&... args) noexcept(std::is_nothrow_constructible_v< value_type, Args... >) -> value_type & |
constructs the contained value in-place | |
constexpr auto | operator== (const strong_type &rhs) const &noexcept(noexcept(m_value==rhs.m_value)) -> bool |
compares gw::strong_type objects | |
constexpr auto | operator< (const strong_type &rhs) const &noexcept(noexcept(m_value< rhs.m_value)) -> bool |
compares gw::strong_type objects | |
constexpr auto | operator> (const strong_type &rhs) const &noexcept(noexcept(m_value > rhs.m_value)) -> bool |
compares gw::strong_type objects | |
constexpr | operator const value_type & () const &noexcept |
converts the gw::strong_type to its underlying type | |
constexpr | operator value_type & () &noexcept |
converts the gw::strong_type to its underlying type | |
constexpr | operator const value_type && () const &&noexcept |
converts the gw::strong_type to its underlying type | |
constexpr | operator value_type && () &&noexcept |
converts the gw::strong_type to its underlying type | |
constexpr auto | operator++ () &noexcept(noexcept(++m_value)) -> strong_type & |
increments the contained value | |
constexpr auto | operator++ () &&noexcept(noexcept(++m_value)) -> strong_type && |
increments the contained value | |
constexpr auto | operator++ (int) &noexcept(noexcept(m_value++)) -> strong_type |
increments the contained value | |
constexpr auto | operator++ (int) &&noexcept(noexcept(m_value++)) -> strong_type |
increments the contained value | |
constexpr auto | operator-- () &noexcept(noexcept(--m_value)) -> strong_type & |
decrements the contained value | |
constexpr auto | operator-- () &&noexcept(noexcept(--m_value)) -> strong_type && |
decrements the contained value | |
constexpr auto | operator-- (int) &noexcept(noexcept(m_value--)) -> strong_type |
decrements the contained value | |
constexpr auto | operator-- (int) &&noexcept(noexcept(m_value--)) -> strong_type |
decrements the contained value | |
constexpr auto | operator+ () const &noexcept(noexcept(+m_value)) -> strong_type |
affirms the contained value | |
constexpr auto | operator+ () &&noexcept(noexcept(+m_value)) -> strong_type |
affirms the contained value | |
constexpr auto | operator- () const &noexcept(noexcept(-m_value)) -> strong_type |
negates the contained value | |
constexpr auto | operator- () &&noexcept(noexcept(-m_value)) -> strong_type |
negates the contained value | |
constexpr auto | operator+ (const strong_type &rhs) const &noexcept(noexcept(m_value+rhs.m_value)) -> strong_type |
adds the contained values | |
constexpr auto | operator+ (strong_type &&rhs) const &noexcept(noexcept(m_value+rhs.m_value)) -> strong_type |
adds the contained values | |
constexpr auto | operator+ (const strong_type &rhs) &&noexcept(noexcept(m_value+rhs.m_value)) -> strong_type |
adds the contained values | |
constexpr auto | operator+ (strong_type &&rhs) &&noexcept(noexcept(m_value+rhs.m_value)) -> strong_type |
adds the contained values | |
constexpr auto | operator- (const strong_type &rhs) const &noexcept(noexcept(m_value - rhs.m_value)) -> strong_type |
subtracts the contained values | |
constexpr auto | operator- (strong_type &&rhs) const &noexcept(noexcept(m_value - rhs.m_value)) -> strong_type |
subtracts the contained values | |
constexpr auto | operator- (const strong_type &rhs) &&noexcept(noexcept(m_value - rhs.m_value)) -> strong_type |
subtracts the contained values | |
constexpr auto | operator- (strong_type &&rhs) &&noexcept(noexcept(m_value - rhs.m_value)) -> strong_type |
subtracts the contained values | |
constexpr auto | operator* (const strong_type &rhs) const &noexcept(noexcept(m_value *rhs.m_value)) -> strong_type |
multiplies the contained values | |
constexpr auto | operator* (strong_type &&rhs) const &noexcept(noexcept(m_value *rhs.m_value)) -> strong_type |
multiplies the contained values | |
constexpr auto | operator* (const strong_type &rhs) &&noexcept(noexcept(m_value *rhs.m_value)) -> strong_type |
multiplies the contained values | |
constexpr auto | operator* (strong_type &&rhs) &&noexcept(noexcept(m_value *rhs.m_value)) -> strong_type |
multiplies the contained values | |
constexpr auto | operator/ (const strong_type &rhs) const &noexcept(noexcept(m_value/rhs.m_value)) -> strong_type |
devides the contained values | |
constexpr auto | operator/ (strong_type &&rhs) const &noexcept(noexcept(m_value/rhs.m_value)) -> strong_type |
devides the contained values | |
constexpr auto | operator/ (const strong_type &rhs) &&noexcept(noexcept(m_value/rhs.m_value)) -> strong_type |
devides the contained values | |
constexpr auto | operator/ (strong_type &&rhs) &&noexcept(noexcept(m_value/rhs.m_value)) -> strong_type |
devides the contained values | |
constexpr auto | operator% (const strong_type &rhs) const &noexcept(noexcept(m_value % rhs.m_value)) -> strong_type |
calculates the remainder of the contained values | |
constexpr auto | operator% (strong_type &&rhs) const &noexcept(noexcept(m_value % rhs.m_value)) -> strong_type |
calculates the remainder of the contained values | |
constexpr auto | operator% (const strong_type &rhs) &&noexcept(noexcept(m_value % rhs.m_value)) -> strong_type |
calculates the remainder of the contained values | |
constexpr auto | operator% (strong_type &&rhs) &&noexcept(noexcept(m_value % rhs.m_value)) -> strong_type |
calculates the remainder of the contained values | |
constexpr auto | operator~ () const &noexcept(noexcept(~m_value)) -> strong_type |
inverts the contained value | |
constexpr auto | operator& (const strong_type &rhs) const &noexcept(noexcept(m_value &rhs.m_value)) -> strong_type |
performs binary AND on the contained values | |
constexpr auto | operator& (strong_type &&rhs) const &noexcept(noexcept(m_value &rhs.m_value)) -> strong_type |
performs binary AND on the contained values | |
constexpr auto | operator& (const strong_type &rhs) &&noexcept(noexcept(m_value &rhs.m_value)) -> strong_type |
performs binary AND on the contained values | |
constexpr auto | operator& (strong_type &&rhs) &&noexcept(noexcept(m_value &rhs.m_value)) -> strong_type |
performs binary AND on the contained values | |
constexpr auto | operator| (const strong_type &rhs) const &noexcept(noexcept(m_value|rhs.m_value)) -> strong_type |
performs binary OR on the contained values | |
constexpr auto | operator| (strong_type &&rhs) const &noexcept(noexcept(m_value|rhs.m_value)) -> strong_type |
performs binary OR on the contained values | |
constexpr auto | operator| (const strong_type &rhs) &&noexcept(noexcept(m_value|rhs.m_value)) -> strong_type |
performs binary OR on the contained values | |
constexpr auto | operator| (strong_type &&rhs) &&noexcept(noexcept(m_value|rhs.m_value)) -> strong_type |
performs binary OR on the contained values | |
constexpr auto | operator^ (const strong_type &rhs) const &noexcept(noexcept(m_value ^ rhs.m_value)) -> strong_type |
performs binary XOR on the contained values | |
constexpr auto | operator^ (strong_type &&rhs) const &noexcept(noexcept(m_value ^ rhs.m_value)) -> strong_type |
performs binary XOR on the contained values | |
constexpr auto | operator^ (const strong_type &rhs) &&noexcept(noexcept(m_value ^ rhs.m_value)) -> strong_type |
performs binary XOR on the contained values | |
constexpr auto | operator^ (strong_type &&rhs) &&noexcept(noexcept(m_value ^ rhs.m_value)) -> strong_type |
performs binary XOR on the contained values | |
constexpr auto | operator<< (const strong_type &rhs) const &noexcept(noexcept(m_value<< rhs.m_value)) -> strong_type |
performs binary left shift on the contained values | |
constexpr auto | operator<< (strong_type &&rhs) const &noexcept(noexcept(m_value<< rhs.m_value)) -> strong_type |
performs binary left shift on the contained values | |
constexpr auto | operator<< (const strong_type &rhs) &&noexcept(noexcept(m_value<< rhs.m_value)) -> strong_type |
performs binary left shift on the contained values | |
constexpr auto | operator<< (strong_type &&rhs) &&noexcept(noexcept(m_value<< rhs.m_value)) -> strong_type |
performs binary left shift on the contained values | |
constexpr auto | operator>> (const strong_type &rhs) const &noexcept(noexcept(m_value > > rhs.m_value)) -> strong_type |
performs binary right shift on the contained values | |
constexpr auto | operator>> (strong_type &&rhs) const &noexcept(noexcept(m_value > > rhs.m_value)) -> strong_type |
performs binary right shift on the contained values | |
constexpr auto | operator>> (const strong_type &rhs) &&noexcept(noexcept(m_value > > rhs.m_value)) -> strong_type |
performs binary right shift on the contained values | |
constexpr auto | operator>> (strong_type &&rhs) &&noexcept(noexcept(m_value > > rhs.m_value)) -> strong_type |
performs binary right shift on the contained values | |
constexpr auto | begin () const noexcept(noexcept(std::ranges::begin(m_value))) |
returns an iterator to the beginning of the contained value | |
constexpr auto | begin () noexcept(noexcept(std::ranges::begin(m_value))) |
returns an iterator to the beginning of the contained value | |
constexpr auto | end () const noexcept(noexcept(std::ranges::end(m_value))) |
returns an iterator to the end of the contained value | |
constexpr auto | end () noexcept(noexcept(std::ranges::end(m_value))) |
returns an iterator to the end of the contained value | |
Friends | |
auto | operator<< (std::ostream &ostream, const strong_type &rhs) noexcept(noexcept(ostream<< rhs.m_value)) -> std::ostream & |
inserts formatted data | |
auto | operator>> (std::istream &istream, strong_type &rhs) noexcept(noexcept(istream > > rhs.m_value)) -> std::istream & |
extracts formatted data | |
Strong type wrapper.
The class template gw::strong_type
is a simple wrapper around a value of some type T
. It is intended to be used to create a new type that is distinct from T
, but which has the same representation and semantics as T
. This is useful to distinguish between values that are conceptually different, but which have the same underlying type. For example, a std::string
can be used to represent a person's name, but it can also be used to represent a person's address. Using gw::strong_type
to create a name_t
and an address_t
allows the compiler to catch errors where a name_t
is used where an address_t
is expected, and vice versa.