A light-weight easy-to-use single-file header-only template class for strong typing.
More...
|
|
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) |
| |
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 };