# Strange c++ quirk

## 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

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by DeyjaImagine 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

##### Share on other sites
Quote:
Original post by Polymorphic OOP
Quote:
 Original post by DeyjaImagine 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

##### Share on other sites
Quote:
 Original post by Polymorphic OOPThats 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

##### 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

##### Share on other sites
Quote:
 Original post by FrunyIf 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

##### Share on other sites
Quote:
 Original post by helixI'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

##### Share on other sites
It's just an implicit conversion from NULL to bool, isn't it?

ace

#### Share this post

##### Share on other sites
Quote:
 Original post by helixThis 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

##### Share on other sites
Quote:
 Original post by trevaaarAlso, AFAIK there is no way to make VC7 use the VC6 runtime (if there is, it's probably not a good idea anyway).

Sure there is, it just all depends on how you have your build environment set up. For this project we're basically using VC6 with the VC7 IDE. I would never do that for my own project but since I'm inheriting this one, I'm not about to break something that is working. :)

Quote:
 Original post by ace_lovegroveIt's just an implicit conversion from NULL to bool, isn't it?

That's what I would imagine would happen. NULL is basically 0 which is false.

Quote:
 Original post by njpaulIf 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?

That's what I was getting at. And no, it doesn't work for the reason I'll give below. :p

I went to compile it again to test it and it doesn't compile now (in either Debug or Release). Now I'm confused (although this is what I would have expected to happen).

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

• ### Forum Statistics

• Total Topics
628378
• Total Posts
2982347

• 10
• 9
• 15
• 24
• 11