ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Public Attributes | Friends | List of all members
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

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