Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make typedefs incompatible

Tags:

c++

Situation:

typedef int TypeA;
typedef int TypeB;

I need to make TypeA incompatible with TypeB (so any attempt to assign TypeA to TypeB would trigger compile error), while retaining all functionality provided by built-in type (operators).

One way to do it is to wrap each type into separate struct/class (and redefine all operators, etc).

Is there any other, more "elegant", way to do it?

Third party libraries are not allowed. C++0x/C++11x is not supported. (C++ 2003 is supported)

like image 832
SigTerm Avatar asked Feb 22 '12 11:02

SigTerm


1 Answers

The only way is to create a new type (by using for example BOOST_STRONG_TYPEDEF).

like image 104
BЈовић Avatar answered Oct 27 '22 06:10

BЈовић