Skip to content

Instantly share code, notes, and snippets.

@SergNikitin
Created October 20, 2016 09:15
Show Gist options
  • Save SergNikitin/1666fc116124ed4139f587526f0158b2 to your computer and use it in GitHub Desktop.
Save SergNikitin/1666fc116124ed4139f587526f0158b2 to your computer and use it in GitHub Desktop.
Explicit ID
template <typename T, typename UniqueTag>
class ExplicitArithmeticID
{
public:
static_assert(std::is_arithmetic<T>::value, "ExplicitArithmeticID is for arithmetic types only");
using UnderlyingType = T;
constexpr explicit ExplicitArithmeticID(T value) : mUnderlyingValue(value) {};
const T& underlying() const
{
return mUnderlyingValue;
}
private:
T mUnderlyingValue;
};
struct GeoIDTag {};
using GeoID = ExplicitArithmeticID<int, GeoIDTag>;
struct TerIDTag {};
using TerID = ExplicitArithmeticID<int, TerIDTag>;
void foo(GeoID) {}
int main()
{
foo(GeoID(0));
// foo(TerID(0)); // compilation error
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment