How do I get g++ to make type checks on typedefs? Is it possible? i.e.
typedef int T1;
typedef int T2;
T1 x = 5; //Ok with me
T2 y = x; //Any way to get an error or a warning here?
I can't use C++0x features (I don't know whether they can do this.)
EDIT: What I want is something like this:
typedef int BallID;
typedef int BatID;
BatID x = 10;
map<BatID, Bat*> m;
m.insert(make_pair(x, bigbat)); //OK
BallID y = 15;
m.insert(make_pair(y, smallbat)); //Give me a warning at least plz
Is this too much to ask?
Consider using a strong typedef: https://www.boost.org/doc/libs/release/boost/serialization/strong_typedef.hpp
To expand upon Nawaz's answer: when you typedef A B
, then B
is just an alias for A
, not a separate type. x
and y
are just int
's in your example.
If you want to create a new type, use a one-member struct
.
As long as T1
and T2
are typedefs of same type, you will not get any warning!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With