Veritas Veritas - 1 year ago 131
C++ Question

Strong typedefs

Is there any way to make a complete copy of a type so that they can be distinguished in template deduction context? Take the example:

template <typename T>
static test
static int c()
static int t = 0;
return i++;

typedef int handle;

int main()
std::cout << test<int>::c() << std::endl;
std::cout << test<handle>::c() << std::endl;
return 0;

Since typedef only makes an alias for a type, this prints 0, 1
instead of the desired 0, 0. Is there any workaround for this?

Answer Source


Note that neither typedef nor using create new distinct data types. They only create synonyms of existing types. That means that the type of myword above, declared with type WORD, can as well be considered of type unsigned int; it does not really matter, since both are actually referring to the same type.

Since int and handle are one and the same, the output 0 1 is expected.

There's a workaround though, as @interjay suggests.


BOOST_STRONG_TYPEDEF( int , handle );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download