ampsci
High-precision calculations for one- and two-valence atomic systems
Loading...
Searching...
No Matches
qip::StrongType< enumV, BaseT >

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

#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 StrongToperator*= (const StrongT &rhs)
 Provides operators for regular arithmetic operations.
 
constexpr StrongToperator/= (const StrongT &rhs)
 
constexpr StrongToperator+= (const StrongT &rhs)
 
constexpr StrongToperator-= (const StrongT &rhs)
 
constexpr StrongToperator*= (const BaseT &rhs)
 Provide Base*Strong, Strong*Base oprators - allow scalar multiplication.
 
constexpr StrongToperator/= (const BaseT &rhs)
 Provide Strong/Base, but NOT Base/Strong (still scalar multiplication).
 
constexpr StrongToperator++ ()
 Provides pre/post increment/decrement (++, –) operators.
 
constexpr StrongT operator++ (int)
 
constexpr StrongToperator-- ()
 
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 operators for direct comparison w/ BaseT literal (rvalue). Note: Does not allow comparison with BaseT lvalue.
 
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)
 

Detailed Description

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:45

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

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

Provide Base*Strong, Strong*Base oprators - allow 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 operators for direct comparison w/ BaseT literal (rvalue). Note: Does not allow comparison with BaseT lvalue.

◆ 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: