Utilities
Utilities header only library
utils::StrongType< ValueType, PhantomType > Struct Template Reference

Template class implementation of a Named Strong Type. More...

Inheritance diagram for utils::StrongType< ValueType, PhantomType >:
[legend]

Public Member Functions

 StrongType (ValueType value)
 
template<typename RT >
std::enable_if_t< std::is_arithmetic_v< RT >, RT > As () const
 Cast to a specified type. Only work for arithmetic types.
 
bool operator== (const StrongType &other) const
 
bool operator!= (const StrongType &other) const
 
bool operator< (const StrongType &other) const
 
bool operator> (const StrongType &other) const
 
const ValueType & operator* () const
 Get underlying value using the * operator.
 

Protected Attributes

ValueType value_
 The actual underlying value.
 

Friends

std::ostream & operator<< (std::ostream &out, const StrongType< ValueType, PhantomType > &id)
 

Detailed Description

template<typename ValueType, typename PhantomType>
struct utils::StrongType< ValueType, PhantomType >

Template class implementation of a Named Strong Type.

Note
https://www.fluentcpp.com/2016/12/08/strong-types-for-strong-interfaces/ "A strong type is a type used in place of another type to carry specific meaning through its name. As opposed to strong types would be general-use types, like native types such as ints and double for example. Often, native types do not tell much about the meaning of their instances." It prevents implicit conversions to and from other types, as well as comparisons to other types.
Template Parameters
ValueTypeThe type of the underlying value.
PhantomTypeAn unused type specific for each Strong Type to ensure the Strong Type is different from every other type.