ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
|
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 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) |
Provide Base*Strong, Strong*Base oprators - allow 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 friend StrongT | operator* (StrongT lhs, const StrongT &rhs) |
constexpr friend StrongT | operator/ (StrongT lhs, const StrongT &rhs) |
constexpr friend StrongT | operator+ (StrongT lhs, const StrongT &rhs) |
constexpr friend StrongT | operator- (StrongT lhs, const StrongT &rhs) |
constexpr friend StrongT | operator* (StrongT lhs, const BaseT &rhs) |
constexpr friend StrongT | operator* (const BaseT &lhs, StrongT rhs) |
constexpr friend StrongT | operator/ (StrongT lhs, const BaseT &rhs) |
constexpr friend bool | operator== (const StrongT &lhs, const StrongT &rhs) |
Provides comparison operators. | |
constexpr friend bool | operator!= (const StrongT &lhs, const StrongT &rhs) |
constexpr friend bool | operator< (const StrongT &lhs, const StrongT &rhs) |
constexpr friend bool | operator> (const StrongT &lhs, const StrongT &rhs) |
constexpr friend bool | operator<= (const StrongT &lhs, const StrongT &rhs) |
constexpr friend bool | operator>= (const StrongT &lhs, const StrongT &rhs) |
constexpr friend 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 friend bool | operator!= (const StrongT &lhs, const BaseT &&rhs) |
constexpr friend bool | operator< (const StrongT &lhs, const BaseT &&rhs) |
constexpr friend bool | operator> (const StrongT &lhs, const BaseT &&rhs) |
constexpr friend bool | operator<= (const StrongT &lhs, const BaseT &&rhs) |
constexpr friend bool | operator>= (const StrongT &lhs, const BaseT &&rhs) |
constexpr friend bool | operator== (const BaseT &&lhs, const StrongT &rhs) |
constexpr friend bool | operator!= (const BaseT &&lhs, const StrongT &rhs) |
constexpr friend bool | operator< (const BaseT &&lhs, const StrongT &rhs) |
constexpr friend bool | operator> (const BaseT &&lhs, const StrongT &rhs) |
constexpr friend bool | operator<= (const BaseT &&lhs, const StrongT &rhs) |
constexpr friend 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) |
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.
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.
Usage is best shown with examples. Consider this dummy problem, where we define strong types for Energy, Mass, and Velocity.