Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "bool const a( 5 )" in "Microsoft Visual Studio C++ 2010"

Reply
Thread Tools

"bool const a( 5 )" in "Microsoft Visual Studio C++ 2010"

 
 
Stefan Ram
Guest
Posts: n/a
 
      09-28-2011
I compiled and executed (via Ctrl-F5)

#include <iostream>
#include <ostream>

int main(){ bool const a( 5 ); ::std::cout <<( a == true )<< '\n'; }

in »Microsoft Visual Studio 2010 C++ Express, Version 10.0.30319.1 RTMRel«
(a recent version) with the default settings (the Debug configuration as
preconfigured).

It printed:

0

. But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
conversion about non-zero values:

»any other value is converted to true.«

?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-28-2011
On 28.09.2011 16:40, Stefan Ram wrote:
> I compiled and executed (via Ctrl-F5)
>
> #include<iostream>
> #include<ostream>
>
> int main(){ bool const a( 5 ); ::std::cout<<( a == true )<< '\n'; }
>
> in »Microsoft Visual Studio 2010 C++ Express, Version 10.0.30319.1 RTMRel«
> (a recent version) with the default settings (the Debug configuration as
> preconfigured).
>
> It printed:
>
> 0


I have confirmed this behavior with MSVC 10.0.


> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
> conversion about non-zero values:
>
> »any other value is converted to true.«
>
> ?


It's a Visual C++ compiler bug.

g++ does not have this bug.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Balog Pal
Guest
Posts: n/a
 
      09-28-2011
"Stefan Ram" <(E-Mail Removed)-berlin.de>

> I compiled and executed (via Ctrl-F5)
>
> #include <iostream>
> #include <ostream>
>
> int main(){ bool const a( 5 ); ::std::cout <<( a == true )<< '\n'; }
>
> in »Microsoft Visual Studio 2010 C++ Express, Version 10.0.30319.1
> RTMRel«
> (a recent version) with the default settings (the Debug configuration as
> preconfigured).
>
> It printed:
>
> 0
>
> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
> conversion about non-zero values:
>
> »any other value is converted to true.«


Yep. Bug-ridden compiler . VS2008 does the same. It even prints a
warning about converting int to const bool, but then leaves its value at 5,
that can be printed. If you remove 'const' and only use bool, then value is
1 and prints 1 for comarision.

 
Reply With Quote
 
Paul Brettschneider
Guest
Posts: n/a
 
      09-28-2011
Stefan Ram wrote:

> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
> conversion about non-zero values:
>
> »any other value is converted to true.«
>
> ?


Oh yes! This has bitten me numerous times. Erroneously declared a function
parameter as bool instead of int and got very strange results.
 
Reply With Quote
 
Edek
Guest
Posts: n/a
 
      09-28-2011
On 09/28/2011 08:28 PM, Paul Brettschneider wrote:
> Stefan Ram wrote:
>
>> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
>> conversion about non-zero values:
>>
>> »any other value is converted to true.«
>>
>> ?

>
> Oh yes! This has bitten me numerous times. Erroneously declared a function
> parameter as bool instead of int and got very strange results.


I don't think that is the point; it is about the standard conversion
to bool, which many programmers rely on and which is well specified
in the standard.

Edek
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-29-2011
On Wed, 2011-09-28, Balog Pal wrote:
> "Stefan Ram" <(E-Mail Removed)-berlin.de>
>
>> I compiled and executed (via Ctrl-F5)
>>
>> #include <iostream>
>> #include <ostream>
>>
>> int main(){ bool const a( 5 ); ::std::cout <<( a == true )<< '\n'; }
>>
>> in »Microsoft Visual Studio 2010 C++ Express, Version 10.0.30319.1
>> RTMRel«
>> (a recent version) with the default settings (the Debug configuration as
>> preconfigured).
>>
>> It printed:
>>
>> 0
>>
>> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
>> conversion about non-zero values:
>>
>> »any other value is converted to true.«

>
> Yep. Bug-ridden compiler . VS2008 does the same. It even prints a
> warning about converting int to const bool, but then leaves its value at 5,
> that can be printed. If you remove 'const' and only use bool, then value is
> 1 and prints 1 for comarision.


That surprises me -- it's a fairly visible and very dangerous bug, and
I thought the MS compilers were really good these days. Or did I miss
something? (I don't use them myself.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Fulvio Esposito
Guest
Posts: n/a
 
      09-29-2011
mmm in the recently released Developer Preview of
visual Studio 11 the bug is still present! Very
disappointing!!

Cheers,
Fulvio Esposito
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      09-29-2011
"Jorgen Grahn" <(E-Mail Removed)>
>>> int main(){ bool const a( 5 ); ::std::cout <<( a == true )<< '\n'; }

>>
>> Yep. Bug-ridden compiler . VS2008 does the same. It even prints a
>> warning about converting int to const bool, but then leaves its value at
>> 5,
>> that can be printed. If you remove 'const' and only use bool, then value
>> is
>> 1 and prints 1 for comarision.

>
> That surprises me -- it's a fairly visible and very dangerous bug, and
> I thought the MS compilers were really good these days.


Me too. For the record, VS6 does not have this bug. It was picked up on the
way up.


 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      09-29-2011
On 29 syys, 15:13, Jorgen Grahn <(E-Mail Removed)> wrote:
> I thought the MS compilers were really good these days. Or did I miss
> something?


VC++ is teaching to avoid mixing numbers with bool type.
It's a good lesson!
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      09-29-2011
On Wed, 28 Sep 2011 17:04:54 +0200, "Alf P. Steinbach"
<(E-Mail Removed)> wrote:

>On 28.09.2011 16:40, Stefan Ram wrote:
>> I compiled and executed (via Ctrl-F5)
>>
>> #include<iostream>
>> #include<ostream>
>>
>> int main(){ bool const a( 5 ); ::std::cout<<( a == true )<< '\n'; }
>>
>> in »Microsoft Visual Studio 2010 C++ Express, Version 10.0.30319.1 RTMRel«
>> (a recent version) with the default settings (the Debug configuration as
>> preconfigured).
>>
>> It printed:
>>
>> 0

>
>I have confirmed this behavior with MSVC 10.0.
>
>
>> . But didn't ISO/IEC 14882:2011 say in 4.12 Boolean
>> conversion about non-zero values:
>>
>> »any other value is converted to true.«
>>
>> ?

>
>It's a Visual C++ compiler bug.
>
>g++ does not have this bug.
>
>


But VS emits a warning:

warning C4305: 'initializing' : truncation from 'int' to 'const bool'

The compiler is putting the bit into a (mov byte ptr [a],1)
but it's pushing 0 to the cout << operator.
 
Reply With Quote
 
 
 
Reply

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Should I write Visual studio 2005 or Visual studio 2003 MCSD =?Utf-8?B?VmlqYXk=?= Microsoft Certification 14 06-30-2006 09:05 AM
Is Visual Studio Team System and Visual Studio Foundation Server are same?. Thirumalai ASP .Net 0 05-22-2006 08:48 AM
visual studio .net 2003 verses visual studio .net 2002 wh ASP .Net 2 01-16-2004 04:54 PM



Advertisments