Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiom for strict typedef in C++

Tags:

Is there an idiom for a strict typedef in C++, possibly using templates?

Something like:

template <class base_type, int N> struct new_type{
    base_type p;
    explicit new_type(base_type i = base_type()) : p(i) {}
};

typedef new_type<int, __LINE__> x_coordinate;
typedef new_type<int, __LINE__> y_coordinate;

So I can make something like this a compile time error:

x_coordinate x(5);
y_coordinate y(6);

x = y; // whoops

The __LINE__ in there looks like it might be trouble, but I'd prefer not to have to manually create a set of constants merely to keep each type unique.