Strange c++ quirk

Started by
9 comments, last by helix 18 years, 6 months ago
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.
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.
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.
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]
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
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).
It's just an implicit conversion from NULL to bool, isn't it?

ace
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?

This topic is closed to new replies.

Advertisement