By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

g++: integers as booleans, no warning?

P: n/a
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

--
The UNIX Guru's view of Sex:
unzip; strip; touch; finger; mount; fsck; more; yes; umount; sleep
-=-=- -=-=-=-=-
Dipl.Ing. Martin "Herbert" Dietze -=-=- University of Buckingham -=-=-
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Martin Herbert Dietze wrote:
[...] Is there really no way to get warnings out of g++ for
such things?


Why don't you ask in a g++ newsgroup: gnu.g++.help?
Jul 23 '05 #2

P: n/a
Martin Herbert Dietze wrote:
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).
Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

P: n/a

"SnaiL" <VL********@MIRATECH.BIZ> skrev i en meddelelse
news:11*********************@z14g2000cwz.googlegro ups.com...
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.
Thanks.


/Peter
Jul 23 '05 #5

P: n/a
SnaiL wrote:
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.
Jul 23 '05 #6

P: n/a
SnaiL wrote:
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.
Jul 23 '05 #7

P: n/a
Martin Herbert Dietze wrote:
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.

Jul 23 '05 #8

P: n/a
Ron Natalie <ro*@sensor.com> wrote in message news:<42**********************@news.newshosting.co m>...
Martin Herbert Dietze wrote:

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).


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.
Jul 23 '05 #9

P: n/a
SnaiL wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.