gw  1.0.0
A bunch of small C++ utilities
gw::strong_type< T, Tag > Class Template Referencefinal

Strong type wrapper. More...

#include <strong_type.hpp>

Inheritance diagram for gw::strong_type< T, Tag >:
[legend]
Collaboration diagram for gw::strong_type< T, Tag >:
[legend]

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>
constexpr strong_type (Args &&... args) noexcept(std::is_nothrow_constructible_v< T, Args... >) requires std
 constructs the gw::strong_type object
 
template<typename U , typename... Args>
constexpr strong_type (std::initializer_list< U > ilist, Args &&... args) noexcept(std::is_nothrow_constructible_v< T, std::initializer_list< U > &, Args... >) requires std
 constructs the gw::strong_type object
 
 ~strong_type () noexcept(std::is_nothrow_destructible_v< T >)=default
 destroys the contained value
 
constexpr auto operator-> () const noexcept -> const T *
 accesses the contained value
 
constexpr auto operator-> () noexcept -> T *
 accesses the contained value
 
constexpr auto operator* () const &noexcept -> const T &
 accesses the contained value
 
constexpr auto operator* () &noexcept -> T &
 accesses the contained value
 
constexpr auto operator* () const &&noexcept -> const T &&
 accesses the contained value
 
constexpr auto operator* () &&noexcept -> T &&
 accesses the contained value
 
constexpr auto value () const &noexcept -> const T &
 returns the contained value
 
constexpr auto value () &noexcept -> T &
 returns the contained value
 
constexpr auto value () const &&noexcept -> const T &&
 returns the contained value
 
constexpr auto value () &&noexcept -> T &&
 returns the contained value
 

Detailed Description

template<typename T, typename Tag>
class gw::strong_type< T, Tag >

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.

Examples
strong_type.cpp.

The documentation for this class was generated from the following file: