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

Thread Tools

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 12:24 PM.

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360