https://github.com/tomaszrewak/strong_typedefs
A strong_typedef implementation for C++ with selective operator overloads.
https://github.com/tomaszrewak/strong_typedefs
cpp header-only library operator-overloading strongly-typed
Last synced: over 1 year ago
JSON representation
A strong_typedef implementation for C++ with selective operator overloads.
- Host: GitHub
- URL: https://github.com/tomaszrewak/strong_typedefs
- Owner: TomaszRewak
- License: mit
- Created: 2023-12-10T23:17:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-11T17:01:32.000Z (over 2 years ago)
- Last Synced: 2025-01-05T07:13:24.747Z (over 1 year ago)
- Topics: cpp, header-only, library, operator-overloading, strongly-typed
- Language: C++
- Homepage:
- Size: 11.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## About
A (yet another) `strong_typedef` implementation for C++.
`strong_typedef`s allow you to create new types that are derived from existing ones, but are not implicitly convertible to them. This allows you to create types that are semantically different, but have the same underlying representation.
Using `strong_typedef`s helps in avoiding some common pitfalls, like accidentally adding a distance to a time or passing a weight to a function that expects a price (where all of those could be represented by a `double`).
This (single-header) library also provides a convenient way of defining common operators for your new types. You can express that it makes sense to multiply a price by a quantity, while preventing the multiplication of two prices.
## Usage
Include the header file:
```cpp
#include "strong_typedef.hpp"
```
Define your new types:
```cpp
using price = strong_typedef::type;
using quantity = strong_typedef::type;
```
Express which operations are allowed and what are their result types:
```cpp
template <>
struct strong_typedef::operators // price □ price
{
using add = price; // price + price => price
using subtract = price; // price - price => price
};
template <>
struct strong_typedef::operators // price □ quantity
{
using multiply = price; // price * quantity => price
using divide = price; // price / quantity => price
};
template <>
struct strong_typedef::operators // □ price
{
using minus = price; // - price => price
};
```
You can now use your new types:
```cpp
void use(price price, quantity quantity) { /**/ }
int main()
{
price p{1.0};
quantity v{3};
use(p, v); // OK
use(p * v, v); // OK
use(-p, v); // OK
use(price{1}, quantity{3}); // OK
use(p, quantity{p.get() + v.get()}); // OK, on your own risk
use(v, p); // Compile-time error (cannot pass a price as a quantity and vice versa)
use(p + v, v); // Compile-time error (cannot add quantity to a price)
use(1, 3); // Compile-time error (cannot implicitly convert between a number and price/quantity)
}
```
You can retrieve the internal value using the `get()` method:
```cpp
price p{1.0};
double d = p.get();
```
The list of supported operators is:
```cpp
template
struct strong_typedef::operators
{
// For strong_typedef::operators
using add = _operator_undefined;
using subtract = _operator_undefined;
using multiply = _operator_undefined;
using divide = _operator_undefined;
using modulo = _operator_undefined;
using equal = _operator_undefined;
using not_equal = _operator_undefined;
using less = _operator_undefined;
using less_equal = _operator_undefined;
using greater = _operator_undefined;
using greater_equal = _operator_undefined;
using logical_and = _operator_undefined;
using logical_or = _operator_undefined;
// For strong_typedef::operators
using plus = _operator_undefined;
using minus = _operator_undefined;
using logical_not = _operator_undefined;
};
```