g++: integers as booleans, no warning?

  • Thread starter Martin Herbert Dietze
  • Start date
M

Martin Herbert Dietze

Hello,

I just stumbled across a nasty bug caused by default arguments
and expressions of type int or pointer accepted as bool in
function parameter lists like:

| void func1 (int foo, bool bar, char *ch == NULL);
|
| [...]
|
| func1 (42, NULL);


| void func2 (int foo, bool bar, unsigned u == 0);
|
| [...]
|
| func2 (42, 0x42);

This does not generate a diagnostic message in `g++ -Wall -ansi
-pedantic'. OK, this is legal code, however it is also potentially
harmful. Is there really no way to get warnings out of g++ for
such things?

Cheers,

Martin
 
R

Ron Natalie

Martin said:
Hello,

I just stumbled across a nasty bug caused by default arguments
and expressions of type int or pointer accepted as bool in
function parameter lists like:

This is by design. Both convert to bool with true meaning non-zero
(or non-null for poitners).
| void func1 (int foo, bool bar, char *ch == NULL);
|
| [...]
|
| func1 (42, NULL);

NULL isn't a pointer. It's a integral constant 0.
| void func2 (int foo, bool bar, unsigned u == 0);
|
| [...]
|
| func2 (42, 0x42);

This does not generate a diagnostic message in `g++ -Wall -ansi
-pedantic'. OK, this is legal code, however it is also potentially
harmful. Is there really no way to get warnings out of g++ for
such things?

Don't know, visual C++ gives sort of a warning (it warns that int->bool
conversions have lousy performance).
 
S

SnaiL

Victor, why don't you ask there... why don't you ask here... and
somewhere else :) Reply to the people with some useful information.

Thanks.
 
V

Victor Bazarov

SnaiL said:
Victor, why don't you ask there... why don't you ask here... and
somewhere else :) Reply to the people with some useful information.

SnaiL, don't tell me what to do. Compiler-specific and OS-specific
posts are OFF-TOPIC here. And I will mention it to anybody who posts
off-topic here IF I WANT TO. Get it? Thanks.
 
P

Peter Koch Larsen

SnaiL said:
Victor, why don't you ask there... why don't you ask here... and
somewhere else :) Reply to the people with some useful information.

Victors information was quite useful, actually. He directed the original
poster to ask in tha appropriate newsgroup. This should give better answers
and reduce noise for all.

/Peter
 
R

Ron Natalie

SnaiL said:
Victor, why don't you ask there... why don't you ask here... and
somewhere else :) Reply to the people with some useful information.

Thanks.
The useful information is that if you want to gripe about features
that (in your opinion) are missing from G++ which have nothing whatsoever
to do with C++ in general, you're better advised to try a GCC group.
There are better experts in the vagaries of that compiler there.

Warnings are really off-topic here. No compiler is required to produce
anything messages for well-formed programs.
 
O

Old Wolf

Martin said:
Hello,

I just stumbled across a nasty bug caused by default arguments
and expressions of type int or pointer accepted as bool in
function parameter lists like:

| void func1 (int foo, bool bar, char *ch == NULL);

I suppose you meant '=' instead of '=='
|
| [...]
|
| func1 (42, NULL);


| void func2 (int foo, bool bar, unsigned u == 0);
|
| [...]
|
| func2 (42, 0x42);

This does not generate a diagnostic message in `g++ -Wall -ansi
-pedantic'. OK, this is legal code, however it is also potentially
harmful. Is there really no way to get warnings out of g++ for
such things?

Your problem is caused by int->bool default conversions (recall
that NULL has type 'int' in C++). You could force the second
parameter to be bool by making a wrapper class that disables
default conversions, eg:

class Bool
{
bool b;
template<typename T> Bool(T t) {}
public:
Bool(bool b_): b(b_) {}
operator bool() { return b; }
};

void func2(int foo, Bool bar, unsigned u = 0)
{

Then you can use 'bar' as if it were an ordinary bool,
but if you call func2 with anything other than an exact
bool, you will get a compiler error about the constructor
being private.
 
D

Dylan Nicholson

Ron Natalie said:
Don't know, visual C++ gives sort of a warning (it warns that int->bool
conversions have lousy performance).

But you know what, virtually every time it's given me that warning
it's because I'd made a mistake in the code, and didn't intend such a
conversion to happen automatically. In many cases, I probably would
never have spotted the error otherwise (except after tracking down
some obscure bug weeks later).
On that basis, I would think it should be a highly recommended warning
for any implementation. I don't know if the standard has "warning
recommendations" though.
 
K

Karl Heinz Buchegger

SnaiL said:
Victor, why don't you ask there... why don't you ask here... and
somewhere else :) Reply to the people with some useful information.

You mean such as yourself?
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top