Re: Looking for a warning when casting 'const char *' to bool.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Drew,

> I've done some searching and haven't found the right flag to issue a warning
> in the following scenario.  When passing a 'const char *' to an overloaded
> function that takes either a 'char *' or bool, the compiler is casting the
> 'const char *' to a bool and no warning is issued with the following flags:
> -Wall -Wextra -pedantic.  I tried a mix of a bunch of other warning flags and
> no success.  Is there a way for g++ to issue a warning in this case?

No, since pointers readily (implicitly) convert to bool -- by design as per
the ISO 14882 standard -- there's no way to avoid that conversion in the
situation you described.

char const* --> bool
char const* !--> char*

Since there is only one selectable routine, there is no ambiguity.  Hence,
no warning (or error).

One workaround I've used is to make my own Bool class that isn't "bool
promiscuous", but that does not appear to be viable / desirable in the
example code you provided.

For your test case, the issue is that the code fails const correctness.

Also, with -Wwrite-strings you should see a warning for this line...
  char* c1 = "first arg";
...since string literals are const.

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux