implicit conversion of bool to int

T

tim

We've started to use a coding standards checker at work. The following
code results in a warning:

38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool
bSaveLast)
39: :CLog()//call base class constructor to initialize members
40: {
41:
42: m_bTimeStamp=bTimeStamp;
43: try
44: {
45:
46:
47: if( true == bSaveLast )
^
Msg(2:3051) Be aware that an implicit conversion from 'bool' to 'int'
takes place.
48: {
49: CLog::SaveLastRunLog( szPath );
50: }
51:


The warning is flagged at line 47. Since the parameter is type bool,
and the 'true' is a bool, is there an implicit conversion taking place?

Yes, I've just spotted that the sub-class attributes arent in the
initialization list :(

===

I'm also interested in how others have found coding standard checkers.
Have they improved productivity, maintainability?

Thanks

Tim
 
R

Rolf Magnus

We've started to use a coding standards checker at work. The following
code results in a warning:

38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool
bSaveLast)
39: :CLog()//call base class constructor to initialize members
40: {
41:
42: m_bTimeStamp=bTimeStamp;
43: try
44: {
45:
46:
47: if( true == bSaveLast )
^
Msg(2:3051) Be aware that an implicit conversion from 'bool' to 'int'
takes place.
48: {
49: CLog::SaveLastRunLog( szPath );
50: }
51:


The warning is flagged at line 47. Since the parameter is type bool,
and the 'true' is a bool, is there an implicit conversion taking place?

Yes. bool is an integral type (3.9.1p7), which means it undergoes integral
promotions (3.9.1p6). This in turn means that bool values get converted to
int when using comparison operators on them (13.6p12).
So true and bSaveLast are first both converted to int, and then the results
are compared.
I'm also interested in how others have found coding standard checkers.
Have they improved productivity, maintainability?

Sorry, I have no experience with any of those.
 
A

Alf P. Steinbach

* (e-mail address removed):
We've started to use a coding standards checker at work. The following
code results in a warning:

38: CPlainText::CPlainText(const char *szPath,bool bTimeStamp,bool
bSaveLast)
39: :CLog()//call base class constructor to initialize members
40: {
41:
42: m_bTimeStamp=bTimeStamp;
43: try
44: {
45:
46:
47: if( true == bSaveLast )
^
Msg(2:3051) Be aware that an implicit conversion from 'bool' to 'int'
takes place.
48: {
49: CLog::SaveLastRunLog( szPath );
50: }
51:


The warning is flagged at line 47. Since the parameter is type bool,
and the 'true' is a bool, is there an implicit conversion taking place?

Yes, AFAICT. bool is an integral type, an integral type is an
arithmetic type, and '==' applies the "usual arithmetic conversions" on
operands of arithmetic and enumeration type. The usual arithmetic
conversions include integral promotion, which includes the conversion
bool -> int.

Instead of 'true == something', write just 'something'.

Btw., I wouldn't name a boolean "timestamp", because that is just an
association, that it's somehow related to timestamping. And I would
certainly not use Hungarian notation! E.g, name it 'm_addTimestamp'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top