Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 09-09-2004, 04:19 PM   #1 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
[C++] struct typing issues

Why is it that this code (correctly, in my opinion) fails to compile:
Code:
struct Foo {
	int value;
};

struct Bar {
	int value;
};

Foo foo;
Bar bar = foo;
Yet this code compiles just fine.
Code:
template<typename T> struct Value {
	T value;
};

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

Foo foo;
Bar bar = foo;
Is there anyway to get struct typing behaviour with templates? Why does C++ behave this way? If it helps, I'm using gcc 3.2.3
Thanks!
KnifeMissile is offline  
Old 09-09-2004, 06:35 PM   #2 (permalink)
Upright
 
I don't mean this to be harsh, but you need to know where you stand. The fact that you think the first example should work shows you lack a fundamental understand of what you are actually doing.

A struct is a new type. Its just a representation of how a chunk of memory of a particular size should be parsed. Even if two structs are identical, you can't expect the compiler to notice and allow implicit casting.

Its the same as:

Code:
int foo;
float bar;
foo = bar
Its a type mismatch.

Now, if the structs are identical, and you want to futz around with pointers a bit, you might have some luck.

Code:
Foo foo;
Bar *bar = (Bar*)&foo;
That would probably build, and might work. But is ickybad voodoo. Stuff you get fired for doing in the real world.

As for the "right" way to do it... Well, remember, in c++ a struct is a just a class with all public members. So, you could either create a copy constructor, or overload the equals operator. Consult your c++ book for details, or google it up.

Chances are, you don't actually want to do what you are trying. There is probably a more direct way to solve whatever problem you are working at.

As far as the template stuff working.... Templates are one of the features of C++ that makes Java a better OO language.
bacon is offline  
Old 09-09-2004, 11:43 PM   #3 (permalink)
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  
Old 09-09-2004, 11:45 PM   #4 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
Perhaps I do mean to be harsh when I say this but you should really learn to read. Can you see why:
Quote:
The fact that you think the first example should work shows you lack a fundamental understand of what you are actually doing.
is not an apporpriate response to the sentence
Quote:
Why is it that this code (correctly, in my opinion) fails to compile:
As you can see, I did not expect the first example to work. If you are going to risk being harsh, you should make sure that you know what you are talking about...


Now, for clarification (at least one of you seems to need it), I expect different struct declarations to be considered different types (hence, my statement about "(correctly, IMO) fails to compile"). My question was why templated structs aren't considered different types...

It's tempting to debate the merits of C++ versus Java but that would honestly diverge from the thread topic and risk starting a religious flame war...
KnifeMissile is offline  
Old 09-10-2004, 12:44 AM   #5 (permalink)
Junkie
 
Location: San Francisco
Has been answered, but I typed it and posted it and its staying. =P
Quote:
Originally Posted by KnifeMissile
Now, for clarification (at least one of you seems to need it), I expect different struct declarations to be considered different types (hence, my statement about "(correctly, IMO) fails to compile"). My question was why templated structs aren't considered different types...

It's tempting to debate the merits of C++ versus Java but that would honestly diverge from the thread topic and risk starting a religious flame war...
Different declarations are considered different types in the same way for templated classes, but those two code segments are not really equivalent. In the first example, Foo and Bar are two completely seperate types. In the second, Value<int> is the one and only type; the typedef statements don't create new types, just new names for the same one. An equivalent example for the templated class would be something like:

Code:
template<class T> struct s1
{
T x;
};

template<class T> struct s2
{
T x;
};

typedef s1<int> Foo;
typedef s2<int> Bar;

Then
Code:
Foo f;
Bar b = f;
wouldn't compile either for the same reason.

Last edited by n0nsensical; 09-10-2004 at 12:47 AM..
n0nsensical is offline  
Old 09-10-2004, 04:23 AM   #6 (permalink)
Upright
 
I misread. Foot is tasty.
bacon is offline  
 

Tags
issues, struct, typing


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:29 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

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