Jump to content
  • Advertisement
Sign in to follow this  
helix

Strange c++ quirk

This topic is 4629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just noticed some silly code I wrote that compiled silently and ran as I expected it to. But...why? In a class' initialization list, I had the following code:
Myclass::Myclass() : m_bBooleanVar1(bool), m_bBooleanVar2(NULL)
Obviously it's getting late and I'm tired but how did this compile and run? The bit with NULL makes perfect sense to me and I just included it for fun, but how can you initialize a variable with a type? Furthermore, I intended to init it with a false and my code executed as if that was the case. I would think if anything, it would be set to true. I'm compiling at warning level 3. At any rate, that should generate a compiler error or at least warning IMO.

Share this post


Link to post
Share on other sites
Advertisement
Imagine for a moment that the variables were some other type. constructor : instance(SomeClass) In this case, SomeClass is equivilant to SomeClass(), which creates an unamed temporary of type SomeClass. This is the same with bool. Now then; the bit with it being initialized to zero is probably just dumb luck.

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
Imagine for a moment that the variables were some other type. constructor : instance(SomeClass) In this case, SomeClass is equivilant to SomeClass(), which creates an unamed temporary of type SomeClass. This is the same with bool. Now then; the bit with it being initialized to zero is probably just dumb luck.

Thats not true at all -- neither for built-in types or for user-defined types. His compiler is just broken and allows non-standard code. Any compliant compiler should give error there. Also, in cases where you do bool(), it's not dumb-luck that the value is 0, it's actually standard that the value is 0-initialized.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Quote:
Original post by Deyja
Imagine for a moment that the variables were some other type. constructor : instance(SomeClass) In this case, SomeClass is equivilant to SomeClass(), which creates an unamed temporary of type SomeClass. This is the same with bool. Now then; the bit with it being initialized to zero is probably just dumb luck.

Thats not true at all -- neither for built-in types or for user-defined types. His compiler is just broken and allows non-standard code. Any compliant compiler should give error there.


Indeed.

Quote:


Error E2188 bool.cpp 9: Expression syntax in function MyClass::MyClass()
Error E2275 bool.cpp 13: { expected in function MyClass::MyClass()
*** 2 errors in Compile ***




I'm rather surprised that that compiles under anything... As you say, it's silly code. I find it mildly worrying how the compiler can not catch such an error.
It must be VC6 [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Thats not true at all -- neither for built-in types or for user-defined types. His compiler is just broken and allows non-standard code. Any compliant compiler should give error there. Also, in cases where you do bool(), it's not dumb-luck that the value is 0, it's actually standard that the value is 0-initialized.


If I remember correctly, you're not even allowed to do bool(), though you're allowed to do T() where T is a template parameter which might be bool.

Share this post


Link to post
Share on other sites
I'm using VC7 with VC7 libs (although, hmm...maybe for that project it is using VC6 libs and runtime...). This was a debug build so maybe it was initing it to 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
If I remember correctly, you're not even allowed to do bool(), though you're allowed to do T() where T is a template parameter which might be bool.

No, it's allowable. You're probably thinking of explicitly calling the destructor of built-in types, which is allowable when the destructor name is a template argument or a typedef but not when using the keyword itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by helix
I'm using VC7 with VC7 libs (although, hmm...maybe for that project it is using VC6 libs and runtime...). This was a debug build so maybe it was initing it to 0.


I don't know about VC7, but in VC7.1 I got a few errors when I tried to compile some code that used bool (that compiled fine with bool() and false). Also, AFAIK there is no way to make VC7 use the VC6 runtime (if there is, it's probably not a good idea anyway).

Share this post


Link to post
Share on other sites
Quote:
Original post by helix
This was a debug build so maybe it was initing it to 0.


If I remember, the debug build initializes every uninitialized variable to 0, whereas the release build will leave them uninitialized (and can cause headaches, so initialize your variables). I'm curious, does it work under release build?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!