2.10.0
Freundlich's C++ toolkit
Classes | Macros | Typedefs | Functions
fcppt.strong_typedef

Description

The strong typedef class and helper macros.

Motivation

C++ offers typedefs to provide aliases (different names) for a type. Typedefs are useful to shorten complicated types and to make type usage consistent. However, they do not prevent accidental misuse. Consider:

void f(std::string first_name);
void g()
{
std::string email_address = "a@b.com";
f(email_address);
}

Accidents like this can be prevented by creating new types first_name and email_address that are wrappers around strings. Because both of them are distinct types, they cannot be implicitly converted into each other.

Strong typedefs are thin wrappers around types that are easy to define:

first_name
);
email_address
);
void f(first_name);
void g()
{
email_address address("a@b.com");
//f(address); error
}

Parameters

Strong typedefs are especially helpful to distinguish function parameters at the type level. Consider the following function:

void f(int vertex_count,bool enable_culling,char character);

Even with parameter names in the function's declaration, the interface is easy to misuse. For example, the following code will compile (possibly without any warnings):

f(true,100,'c');

Instead, we introduce strong typedefs for the parameters.

int,
vertex_count
);
bool,
enable_culling
);
char,
draw_char
);
void f(vertex_count,enable_culling,draw_char);

The correct function call then looks like this:

f(
vertex_count(100),
enable_culling(true),
draw_char('c')
);

Internally, the constructor of strong typedef uses fcppt::cast::safe_numeric which catches unsafe conversions at compile-time. For example, the following code will not compile:

f(vertex_count(true),enable_culling('c'),draw_char(100));

Operations

The get function extracts the value of a strong typedef.

void
f(first_name const _name)
{
std::cout << _name.get() << '\n';
}

Strong typedef wraps almost all operators, which are found in their respective headers.

int,
);
void f(distance const _d1, distance const _d2)
{
std::cout << (_d1 - _d2) << '\n';
}

Internals

When declaring two strong typedefs with the same underlying type, two distinct types need to be created:

int,
first
);
int,
second
);
static_assert(
!std::is_same<first,second>::value,
"Both types must be different"
);

To achieve that, the FCPPT_MAKE_STRONG_TYPEDEF macro first creates an empty struct containing the name you specify as the first parameter. This is then used as the second parameter to fcppt::strong_typedef to make it a distinct type.

Header files

Header file Description
strong_typedef_fwd.hpp Contains fcppt::strong_typedef's declaration.
strong_typedef_decl.hpp Contains fcppt::strong_typedef's definition.
strong_typedef_impl.hpp Contains the definition of fcppt::strong_typedef's member functions.
strong_typedef.hpp Includes strong_typedef_impl.hpp, strong_typedef_operators.hpp and strong_typedef_std_hash.hpp.
strong_typedef_operators.hpp Includes strong_typedef_arithmetic.hpp, strong_typedef_assignment.hpp, strong_typedef_bitwise.hpp and strong_typedef_comparison.hpp.
strong_typedef_input.hpp Contains operator>>.
strong_typedef_output.hpp Contains operator<<.
make_strong_typedef.hpp Contains FCPPT_MAKE_STRONG_TYPEDEF.

Classes

struct  fcppt::is_strong_typedef< T >
 Tests if a type is an fcppt::strong_typedef. More...
 
class  fcppt::strong_typedef< T, Tag >
 Used to create strong typedefs. More...
 
struct  fcppt::strong_typedef< Type, Alias >>
 Hash function object for strong typedefs. More...
 

Macros

#define FCPPT_MAKE_STRONG_TYPEDEF(type, name)
 Creates a new strong typedef using strong_typedef. More...
 

Typedefs

template<typename StrongTypedef >
using fcppt::strong_typedef_tag = typename fcppt::detail::strong_typedef_tag< StrongTypedef >::type
 The tag type of a strong typedef. More...
 

Functions

template<typename StrongTypedef1 , typename Function , typename... StrongTypedefs>
auto fcppt::strong_typedef_apply (Function const &_function, StrongTypedef1 &&_strong_typedef1, StrongTypedefs &&... _strong_typedefs) -> fcppt::strong_typedef< decltype(_function(fcppt::move_if_rvalue< StrongTypedef1 >(_strong_typedef1.get()), fcppt::move_if_rvalue< StrongTypedefs >(_strong_typedefs.get())...)), fcppt::strong_typedef_tag< fcppt::type_traits::remove_cv_ref_t< StrongTypedef1 > > >
 Applies a function to multiple strong typedefs. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator+ (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Add two strong typedefs. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator- (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Subtract two strong typedefs. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator* (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Multiply two strong typedefs. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator- (fcppt::strong_typedef< T, Tag > const &_value)
 Unary minus. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator++ (fcppt::strong_typedef< T, Tag > &_value)
 Preincrement operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator-- (fcppt::strong_typedef< T, Tag > &_value)
 Predecrement operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator++ (fcppt::strong_typedef< T, Tag > &_value, int)
 Postincrement operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator-- (fcppt::strong_typedef< T, Tag > &_value, int)
 Postdecrement operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator+= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Add and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator-= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Subtract and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator*= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Multiply and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator&= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise and and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator|= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise or and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > & fcppt::operator^= (fcppt::strong_typedef< T, Tag > &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise xor and assign operator. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator & (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise and. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator| (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise or. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator^ (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Bitwise xor. More...
 
template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag > fcppt::operator~ (fcppt::strong_typedef< T, Tag > const &_value)
 Bitwise negation. More...
 
template<typename T , typename Tag >
bool fcppt::operator< (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator less. More...
 
template<typename T , typename Tag >
bool fcppt::operator<= (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator less equal. More...
 
template<typename T , typename Tag >
bool fcppt::operator> (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator greater. More...
 
template<typename T , typename Tag >
bool fcppt::operator>= (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator greater equal. More...
 
template<typename T , typename Tag >
bool fcppt::operator== (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator equal. More...
 
template<typename T , typename Tag >
bool fcppt::operator!= (fcppt::strong_typedef< T, Tag > const &_left, fcppt::strong_typedef< T, Tag > const &_right)
 Operator not equal. More...
 
template<typename StrongTypedef , typename Conv , typename Arg >
StrongTypedef fcppt::strong_typedef_construct_cast (Arg const &_arg)
 Applies a cast from fcppt.cast and then construct the strong typedef. More...
 
template<typename Ch , typename Traits , typename T , typename Tag >
std::basic_istream< Ch, Traits > & fcppt::operator>> (std::basic_istream< Ch, Traits > &_stream, fcppt::strong_typedef< T, Tag > &_value)
 Input operator for strong typedefs. More...
 
template<typename StrongTypedef , typename Function >
auto fcppt::strong_typedef_map (StrongTypedef &&_input, Function const &_function) -> fcppt::strong_typedef< decltype(_function(fcppt::move_if_rvalue< StrongTypedef >(_input.get()))), fcppt::strong_typedef_tag< fcppt::type_traits::remove_cv_ref_t< StrongTypedef > > >
 Maps over a strong typedef. More...
 
template<typename Ch , typename Traits , typename T , typename Tag >
std::basic_ostream< Ch, Traits > & fcppt::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::strong_typedef< T, Tag > const &_value)
 Output operator for strong typedefs. More...
 

Macro Definition Documentation

◆ FCPPT_MAKE_STRONG_TYPEDEF

#define FCPPT_MAKE_STRONG_TYPEDEF (   type,
  name 
)
Value:
struct fcppt_strong_typedef_##name##_tag\
{};\
\
type,\
fcppt_strong_typedef_##name##_tag\
fcppt::strong_typedef< fcppt::string,_ > name
The name of a logger.
Definition: name_fwd.hpp:27
Used to create strong typedefs.
Definition: strong_typedef_decl.hpp:43

Creates a new strong typedef using strong_typedef.

Parameters
typeThe type to be wrapped.
nameThe name of the new typedef

Read the module description for more information.

Typedef Documentation

◆ strong_typedef_tag

template<typename StrongTypedef >
using fcppt::strong_typedef_tag = typedef typename fcppt::detail::strong_typedef_tag< StrongTypedef >::type

The tag type of a strong typedef.

Template Parameters
StrongTypedefMust be an fcppt::strong_typedef.

Function Documentation

◆ operator &()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator& ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise and.

◆ operator!=()

template<typename T , typename Tag >
bool fcppt::operator!= ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator not equal.

◆ operator&=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator &= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise and and assign operator.

◆ operator*()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator* ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Multiply two strong typedefs.

◆ operator*=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator*= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Multiply and assign operator.

◆ operator+()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator+ ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Add two strong typedefs.

◆ operator++() [1/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator++ ( fcppt::strong_typedef< T, Tag > &  _value)
inline

Preincrement operator.

◆ operator++() [2/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator++ ( fcppt::strong_typedef< T, Tag > &  _value,
int   
)
inline

Postincrement operator.

◆ operator+=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator+= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Add and assign operator.

◆ operator-() [1/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator- ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Subtract two strong typedefs.

◆ operator-() [2/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator- ( fcppt::strong_typedef< T, Tag > const &  _value)
inline

Unary minus.

◆ operator--() [1/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator-- ( fcppt::strong_typedef< T, Tag > &  _value)
inline

Predecrement operator.

◆ operator--() [2/2]

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator-- ( fcppt::strong_typedef< T, Tag > &  _value,
int   
)
inline

Postdecrement operator.

◆ operator-=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator-= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Subtract and assign operator.

◆ operator<()

template<typename T , typename Tag >
bool fcppt::operator< ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator less.

◆ operator<<()

template<typename Ch , typename Traits , typename T , typename Tag >
std::basic_ostream< Ch, Traits>& fcppt::operator<< ( std::basic_ostream< Ch, Traits > &  _stream,
fcppt::strong_typedef< T, Tag > const &  _value 
)

Output operator for strong typedefs.

◆ operator<=()

template<typename T , typename Tag >
bool fcppt::operator<= ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator less equal.

◆ operator==()

template<typename T , typename Tag >
bool fcppt::operator== ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator equal.

◆ operator>()

template<typename T , typename Tag >
bool fcppt::operator> ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator greater.

◆ operator>=()

template<typename T , typename Tag >
bool fcppt::operator>= ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Operator greater equal.

◆ operator>>()

template<typename Ch , typename Traits , typename T , typename Tag >
std::basic_istream< Ch, Traits>& fcppt::operator>> ( std::basic_istream< Ch, Traits > &  _stream,
fcppt::strong_typedef< T, Tag > &  _value 
)

Input operator for strong typedefs.

◆ operator^()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator^ ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise xor.

◆ operator^=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator^= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise xor and assign operator.

◆ operator|()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator| ( fcppt::strong_typedef< T, Tag > const &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise or.

◆ operator|=()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag>& fcppt::operator|= ( fcppt::strong_typedef< T, Tag > &  _left,
fcppt::strong_typedef< T, Tag > const &  _right 
)
inline

Bitwise or and assign operator.

◆ operator~()

template<typename T , typename Tag >
fcppt::strong_typedef< T, Tag> fcppt::operator~ ( fcppt::strong_typedef< T, Tag > const &  _value)
inline

Bitwise negation.

◆ strong_typedef_apply()

template<typename StrongTypedef1 , typename Function , typename... StrongTypedefs>
auto fcppt::strong_typedef_apply ( Function const &  _function,
StrongTypedef1 &&  _strong_typedef1,
StrongTypedefs &&...  _strong_typedefs 
) -> fcppt::strong_typedef< decltype( _function( fcppt::move_if_rvalue< StrongTypedef1 >( _strong_typedef1.get() ), fcppt::move_if_rvalue< StrongTypedefs >( _strong_typedefs.get() )... ) ), fcppt::strong_typedef_tag< fcppt::type_traits::remove_cv_ref_t< StrongTypedef1 > > >
inline

Applies a function to multiple strong typedefs.

Returns a strong typedef with the same tag type and value _function(_strong_typedef.get(), _strongs_typedefs.get()...).

Template Parameters
StrongTypedef1Must be an fcppt::strong_typedef.
StrongTypedefsMust all be fcppt::strong_typedef.
FunctionMust be a function callable as R (StrongTypedef1::value_type, StrongTypedefs::value_type...), where R is the result type.

◆ strong_typedef_construct_cast()

template<typename StrongTypedef , typename Conv , typename Arg >
StrongTypedef fcppt::strong_typedef_construct_cast ( Arg const &  _arg)

Applies a cast from fcppt.cast and then construct the strong typedef.

◆ strong_typedef_map()

template<typename StrongTypedef , typename Function >
auto fcppt::strong_typedef_map ( StrongTypedef &&  _input,
Function const &  _function 
) -> fcppt::strong_typedef< decltype( _function( fcppt::move_if_rvalue< StrongTypedef >( _input.get() ) ) ), fcppt::strong_typedef_tag< fcppt::type_traits::remove_cv_ref_t< StrongTypedef > > >
inline

Maps over a strong typedef.

Returns a strong typedef with the same tag type and value _function(_input.get()).

Template Parameters
StrongTypedefMust be an fcppt::strong_typedef.
FunctionMust be a function callable as R (StrongTypedef::value_type), where R is the result type.