High-precision calculations for one- and two-valence atomic systems
qip::StrongType< enumV, BaseT >

ok

template<auto enumV, typename BaseT>
struct qip::StrongType< enumV, BaseT >

A light-weight easy-to-use single-file header-only template class for strong typing.

It works using scoped enums; the enum value (+scope) are used to ensure uniqueness for a given user-defined strong type. The class template, defined in the qip namespace, takes an enum class value, and a base type. The base type is confined to be an arithmetic type (float, double, int, long, etc.) to allow for operator overloading. The 'using' declaration is optional, but makes things much easier.

A light-weight easy-to-use single-file header-only template class for strong typing.
Definition StrongType.hpp:44

The newly-defined Strong Type will, for the most part, behave just like an instance of the underlying base type would. In particular, the usual arithmetic operations (+, -, *, /, +=, *= etc.) are all defined, and they work with iostreams. The main difference is that all implicit conversions are banned.

Examples

Usage is best shown with examples. Consider this dummy problem, where we define strong types for Energy, Mass, and Velocity.

#include "StrongType.hpp"
enum class MechanicsTypes { energy, mass, velocity };
// Use of enum class ensures each StrongType is unique

#include <StrongType.hpp>

Public Types

using BaseType = BaseT
 makes 'BaseType' publicly accessible
 

Public Member Functions

constexpr StrongType (BaseT tv)
 
constexpr operator BaseT () const
 
constexpr BaseT & as_base ()
 
constexpr BaseT as_base () const
 
constexpr StrongT & operator*= (const StrongT &rhs)
 Provides operators for regular arithmetic operations.
 
constexpr StrongT & operator/= (const StrongT &rhs)
 
constexpr StrongT & operator+= (const StrongT &rhs)
 
constexpr StrongT & operator-= (const StrongT &rhs)
 
constexpr StrongT & operator*= (const BaseT &rhs)
 Provides Base*Strong and Strong*Base operators for scalar multiplication.
 
constexpr StrongT & operator/= (const BaseT &rhs)
 Provide Strong/Base, but NOT Base/Strong (still scalar multiplication).
 
constexpr StrongT & operator++ ()
 Provides pre/post increment/decrement (++, –) operators.
 
constexpr StrongT operator++ (int)
 
constexpr StrongT & operator-- ()
 
constexpr StrongT operator-- (int)
 

Public Attributes

BaseT v
 

Friends

constexpr StrongT operator* (StrongT lhs, const StrongT &rhs)
 
constexpr StrongT operator/ (StrongT lhs, const StrongT &rhs)
 
constexpr StrongT operator+ (StrongT lhs, const StrongT &rhs)
 
constexpr StrongT operator- (StrongT lhs, const StrongT &rhs)
 
constexpr StrongT operator* (StrongT lhs, const BaseT &rhs)
 
constexpr StrongT operator* (const BaseT &lhs, StrongT rhs)
 
constexpr StrongT operator/ (StrongT lhs, const BaseT &rhs)
 
constexpr bool operator== (const StrongT &lhs, const StrongT &rhs)
 Provides comparison operators.
 
constexpr bool operator!= (const StrongT &lhs, const StrongT &rhs)
 
constexpr bool operator< (const StrongT &lhs, const StrongT &rhs)
 
constexpr bool operator> (const StrongT &lhs, const StrongT &rhs)
 
constexpr bool operator<= (const StrongT &lhs, const StrongT &rhs)
 
constexpr bool operator>= (const StrongT &lhs, const StrongT &rhs)
 
constexpr bool operator== (const StrongT &lhs, const BaseT &&rhs)
 Provides comparison operators with BaseT rvalue literals (not lvalues).
 
constexpr bool operator!= (const StrongT &lhs, const BaseT &&rhs)
 
constexpr bool operator< (const StrongT &lhs, const BaseT &&rhs)
 
constexpr bool operator> (const StrongT &lhs, const BaseT &&rhs)
 
constexpr bool operator<= (const StrongT &lhs, const BaseT &&rhs)
 
constexpr bool operator>= (const StrongT &lhs, const BaseT &&rhs)
 
constexpr bool operator== (const BaseT &&lhs, const StrongT &rhs)
 
constexpr bool operator!= (const BaseT &&lhs, const StrongT &rhs)
 
constexpr bool operator< (const BaseT &&lhs, const StrongT &rhs)
 
constexpr bool operator> (const BaseT &&lhs, const StrongT &rhs)
 
constexpr bool operator<= (const BaseT &&lhs, const StrongT &rhs)
 
constexpr bool operator>= (const BaseT &&lhs, const StrongT &rhs)
 
std::ostream & operator<< (std::ostream &os, const StrongT &rhs)
 Provides iostream interface, works as it would for BaseT.
 
std::istream & operator>> (std::istream &is, StrongT &rhs)
 

Member Typedef Documentation

◆ BaseType

template<auto enumV, typename BaseT >
using qip::StrongType< enumV, BaseT >::BaseType = BaseT

makes 'BaseType' publicly accessible

Member Function Documentation

◆ operator*=() [1/2]

template<auto enumV, typename BaseT >
constexpr StrongT & qip::StrongType< enumV, BaseT >::operator*= ( const StrongT &  rhs)
inlineconstexpr

Provides operators for regular arithmetic operations.

◆ operator*=() [2/2]

template<auto enumV, typename BaseT >
constexpr StrongT & qip::StrongType< enumV, BaseT >::operator*= ( const BaseT &  rhs)
inlineconstexpr

Provides Base*Strong and Strong*Base operators for scalar multiplication.

◆ operator/=()

template<auto enumV, typename BaseT >
constexpr StrongT & qip::StrongType< enumV, BaseT >::operator/= ( const BaseT &  rhs)
inlineconstexpr

Provide Strong/Base, but NOT Base/Strong (still scalar multiplication).

◆ operator++()

template<auto enumV, typename BaseT >
constexpr StrongT & qip::StrongType< enumV, BaseT >::operator++ ( )
inlineconstexpr

Provides pre/post increment/decrement (++, –) operators.

Friends And Related Symbol Documentation

◆ operator== [1/2]

template<auto enumV, typename BaseT >
constexpr bool operator== ( const StrongT &  lhs,
const StrongT &  rhs 
)
friend

Provides comparison operators.

◆ operator== [2/2]

template<auto enumV, typename BaseT >
constexpr bool operator== ( const StrongT &  lhs,
const BaseT &&  rhs 
)
friend

Provides comparison operators with BaseT rvalue literals (not lvalues).

◆ operator<<

template<auto enumV, typename BaseT >
std::ostream & operator<< ( std::ostream &  os,
const StrongT &  rhs 
)
friend

Provides iostream interface, works as it would for BaseT.


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