8

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?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • I'm almost positive this is a dup but I can't find the original. – Mark B Feb 23 '11 at 18:11
  • 1
    No `typedef`, despite its name, does not define a new type, it just creates an alias for an existing type. – sbi Feb 23 '11 at 18:19

3 Answers3

8

Consider using a strong typedef: https://www.boost.org/doc/libs/release/boost/serialization/strong_typedef.hpp

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • +1 for pointing out a useful Boost library that I didn't yet know about. – Fred Foo Feb 23 '11 at 18:14
  • 1
    Thanks. I can't use boost itself due to constraints, but I'll copy-paste the file in my code. But `typedef` just seems stupid to me now. It isn't useful to express an idea about a type is it? – nakiya Feb 23 '11 at 18:26
  • @nakiya Often you just want to create a new name for a type. For example specific instances of standard containers, but you don't need to create an entire new type as the `typedef` name is strictly for convenience and maintainability. – Mark B Feb 23 '11 at 18:29
  • @nakia - it is what it is. Admittedly it's a lot less important now that we have a new interpretation of `auto`, but it's still useful for legibility purposes. – Edward Strange Feb 23 '11 at 18:34
  • @ Mark B, Crazy Eddie - True, but not being able to extend the default type checking for user types sucks, doesn't it? Even the boost workaround feels like awkward. However, it gets the job done I guess. – nakiya Feb 23 '11 at 18:45
  • `typedef` is very useful in generic programming, also in C++0x, since it can be used to create *type members* in classes (e.g. `map::key_type`). – Fred Foo Feb 23 '11 at 18:47
  • @larsmans - No argument. `typedef` may be very useful. But it doesn't do what I wished it would... – nakiya Feb 23 '11 at 18:50
  • 2
    @nakiya: It may not, but that's just not what it was designed to do. In the example I gave, you really want an alias, not a new type. But I'll admit it would be useful if C++ also had an easy way of doing what you want. – Fred Foo Feb 23 '11 at 18:54
  • @nakiya - A more intuitive name, like `typealias`, might be nice but that it works the way it does is sometimes important. – Edward Strange Feb 23 '11 at 19:01
  • 2
    @Crazy: Just like D has `alias`. Sometimes I wish C++ would look there. – GManNickG Feb 23 '11 at 21:21
3

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
2

As long as T1 and T2 are typedefs of same type, you will not get any warning!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    No, you won't get a warning! At least my g++ didn't. – karlphillip Feb 23 '11 at 18:07
  • @karlphillip: See this topic : [Warning : overflow in implicit constant conversion](http://stackoverflow.com/questions/5095434/warning-overflow-in-implicit-constant-conversion) – Nawaz Feb 23 '11 at 18:38
  • Unluckily, this ("no warning") is 100% in accordance with the standard's intent. Which is kinda bullshit because frankly, I cannot figure anyone who would want this behavior (if you didn't want it identifiable as something separate, why would you not just use the builtin type), but it is nevertheless 100% as-intended. Typedef explicitly _does not_ create a separate type, but an alias which is in every respect identical. – Damon Dec 20 '16 at 12:44