44 template <auto enumV,
typename BaseT>
47 static_assert(std::is_arithmetic_v<BaseT>,
48 "StrongType only available for arithmetic types");
50 std::is_enum_v<decltype(enumV)>,
51 "StrongType must be instantiated with scoped enum (enum class)");
57 explicit constexpr
StrongType(BaseT tv) : v(tv) {}
58 explicit constexpr
operator BaseT()
const {
return v; }
59 constexpr BaseT &as_base() {
return v; }
60 [[nodiscard]] constexpr BaseT as_base()
const {
return v; }
70 friend constexpr StrongT operator*(StrongT lhs,
const StrongT &rhs) {
73 constexpr StrongT &operator/=(
const StrongT &rhs) {
77 friend constexpr StrongT operator/(StrongT lhs,
const StrongT &rhs) {
80 constexpr StrongT &operator+=(
const StrongT &rhs) {
84 friend constexpr StrongT operator+(StrongT lhs,
const StrongT &rhs) {
87 constexpr StrongT &operator-=(
const StrongT &rhs) {
91 friend constexpr StrongT operator-(StrongT lhs,
const StrongT &rhs) {
100 friend constexpr StrongT operator*(StrongT lhs,
const BaseT &rhs) {
103 friend constexpr StrongT operator*(
const BaseT &lhs, StrongT rhs) {
114 friend constexpr StrongT operator/(StrongT lhs,
const BaseT &rhs) {
124 StrongT result(*
this);
128 constexpr StrongT &operator--() {
132 constexpr StrongT operator--(
int) {
133 StrongT result(*
this);
140 return lhs.v == rhs.v;
142 friend constexpr
bool operator!=(
const StrongT &lhs,
const StrongT &rhs) {
143 return !(lhs == rhs);
145 friend constexpr
bool operator<(
const StrongT &lhs,
const StrongT &rhs) {
146 return lhs.v < rhs.v;
148 friend constexpr
bool operator>(
const StrongT &lhs,
const StrongT &rhs) {
151 friend constexpr
bool operator<=(
const StrongT &lhs,
const StrongT &rhs) {
154 friend constexpr
bool operator>=(
const StrongT &lhs,
const StrongT &rhs) {
163 friend constexpr
bool operator!=(
const StrongT &lhs,
const BaseT &&rhs) {
166 friend constexpr
bool operator<(
const StrongT &lhs,
const BaseT &&rhs) {
169 friend constexpr
bool operator>(
const StrongT &lhs,
const BaseT &&rhs) {
172 friend constexpr
bool operator<=(
const StrongT &lhs,
const BaseT &&rhs) {
175 friend constexpr
bool operator>=(
const StrongT &lhs,
const BaseT &&rhs) {
178 friend constexpr
bool operator==(
const BaseT &&lhs,
const StrongT &rhs) {
181 friend constexpr
bool operator!=(
const BaseT &&lhs,
const StrongT &rhs) {
184 friend constexpr
bool operator<(
const BaseT &&lhs,
const StrongT &rhs) {
187 friend constexpr
bool operator>(
const BaseT &&lhs,
const StrongT &rhs) {
190 friend constexpr
bool operator<=(
const BaseT &&lhs,
const StrongT &rhs) {
193 friend constexpr
bool operator>=(
const BaseT &&lhs,
const StrongT &rhs) {
201 friend std::istream &operator>>(std::istream &is, StrongT &rhs) {
qip library: A collection of useful functions
Definition: Array.hpp:8
A light-weight easy-to-use single-file header-only template class for strong typing.
Definition: StrongType.hpp:45
constexpr friend bool operator==(const StrongT &lhs, const StrongT &rhs)
Provides comparison operators.
Definition: StrongType.hpp:139
constexpr StrongT & operator*=(const StrongT &rhs)
Provides operators for regular arithmetic operations.
Definition: StrongType.hpp:66
friend std::ostream & operator<<(std::ostream &os, const StrongT &rhs)
Provides iostream interface, works as it would for BaseT.
Definition: StrongType.hpp:198
constexpr friend bool operator==(const StrongT &lhs, const BaseT &&rhs)
Provides operators for direct comparison w/ BaseT literal (rvalue). Note: Does not allow comparison w...
Definition: StrongType.hpp:160
constexpr StrongT & operator*=(const BaseT &rhs)
Provide Base*Strong, Strong*Base oprators - allow scalar multiplication.
Definition: StrongType.hpp:96
constexpr StrongT & operator++()
Provides pre/post increment/decrement (++, –) operators.
Definition: StrongType.hpp:119
constexpr StrongT & operator/=(const BaseT &rhs)
Provide Strong/Base, but NOT Base/Strong (still scalar multiplication).
Definition: StrongType.hpp:110
BaseT BaseType
makes 'BaseType' publicly accessible
Definition: StrongType.hpp:63