View Single Post
Old 09-09-2004, 11:43 PM   #3 (permalink)
roboshark
Crazy
 
I think that KnifeMissile meant that the first piece of code does not compile, and that that is the correct thing. That code fails to compile because a Foo is not a Bar. A compiler can't "look inside" each struct and decide that they are equal even if their names differ. That's an impossible task. Rather, it looks at their types and makes a decision based on that.

In the second example, the type of Foo and Bar is the same:

Code:
typedef Value<int> Foo;
typedef Value<int> Bar;

Foo foo;
Bar bar = foo;
As far as the compiler is concerned a Foo is just a different name for Bar; after all they are both Value<int>'s. That's all. And that's why the code compiles correctly.

Casting struct Foo's to struct Bar's won't necessarily get you fired in the real world as long as you solve a problem that needed to be solved (sometimes you just got to get down and dirty). But it's not a practice I'd advise doing if there's a viable alternative.
__________________
I want no escape.
roboshark is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73