https://github.com/csb6/strong-types
Single-header implementation of strong typedefs in C++
https://github.com/csb6/strong-types
cpp cpp17 strongly-typed typedef
Last synced: 11 months ago
JSON representation
Single-header implementation of strong typedefs in C++
- Host: GitHub
- URL: https://github.com/csb6/strong-types
- Owner: csb6
- License: gpl-3.0
- Created: 2020-05-17T21:24:10.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T20:16:57.000Z (almost 6 years ago)
- Last Synced: 2025-02-17T00:21:09.439Z (about 1 year ago)
- Topics: cpp, cpp17, strongly-typed, typedef
- Language: C++
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Strong-Types
This is an extremely simple, single-header implementation of strong typedefs in C++.
While several good strong typedef libraries for C++ exist, most of them
are complex, making use of a large amount of template metaprogramming to allow levels
of customization that are not necessary for many cases.
This library attempts to offer the core functionality of strong typedefs (creating
typedef-like types that rename an existing type but are treated as distinct types
by the compiler) with as little boilerplate as possible. Here is some sample code
demonstrating the some of the basic features:
```cpp
#include
#include "strong-types.hpp"
struct Width : strong_type {}
struct Height : strong_type {}
int main()
{
Width w{4};
Height h{5};
h = w; // Compile-time error
h += 5;
w *= 9;
Height h2{6};
h = h + h2; // Works
std::cout << h << std::endl; // Works as expected
std::cout << w << std::endl;
return 0;
}
```
## Features
- Simple creation of typedefs; simply create a struct inheriting from `strong_type`,
where `A` is the type you want to typedef and `B` is the type name of the typedef
- Templated operator overloads, including most arithmetic, comparison, and increment/
decrementing operators, as well as an output stream operator. Adding your own operators
is trivial
- To access members/member functions of the underlying type, simply access the
'v' member. If you don't want to see the 'v' member, then simply use private inheritance.
See `example.cpp` and the files in the `tests` directory for more examples of usage.
[This blog post](https://www.fluentcpp.com/2018/04/06/strong-types-by-struct) by Vincent
Zalzal was the inspiration for this simple method of creating strong typedefs.