public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Looking for a warning when casting 'const char *' to bool.
@ 2008-08-22  1:49 Andrew Frezell
  2008-08-22 11:22 ` Eljay Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Frezell @ 2008-08-22  1:49 UTC (permalink / raw)
  To: gcc-help

Hello all,

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?  Below is example code

I know what the correct thing to do is, provide another overloaded function that takes a 'const char *', but this is an existing code base of millions of lines of code and I would like to find anything lurking through a warning.

Thanks,

Drew

// Start example

#include <iostream>

using namespace std;

class Foo {
public:
    void Test(char *c, bool b);
    void Test(char *c1, char *c2);
};

inline void Foo::Test(char *c, bool b) {
    cout << __PRETTY_FUNCTION__ << endl;
    cout << "c = " << c << endl;
    cout << "b = " << b << endl;
}

inline void Foo::Test(char *c1, char *c2) {
    cout << __PRETTY_FUNCTION__ << endl;
    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
}

int main(int argc, char *argv[]) {
    char *c1 = "first arg";
    const char *c2 = "second arg";
    Foo f;

    f.Test(c1, c2);

    return 0;
}


      

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Looking for a warning when casting 'const char *' to bool.
  2008-08-22  1:49 Looking for a warning when casting 'const char *' to bool Andrew Frezell
@ 2008-08-22 11:22 ` Eljay Love-Jensen
  2008-08-23  2:02   ` corey taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Eljay Love-Jensen @ 2008-08-22 11:22 UTC (permalink / raw)
  To: dfrezell, GCC-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Looking for a warning when casting 'const char *' to bool.
  2008-08-22 11:22 ` Eljay Love-Jensen
@ 2008-08-23  2:02   ` corey taylor
  0 siblings, 0 replies; 3+ messages in thread
From: corey taylor @ 2008-08-23  2:02 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: dfrezell, GCC-help

On Fri, Aug 22, 2008 at 7:00 AM, Eljay Love-Jensen <eljay@adobe.com> wrote:
> 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.

Although that work work, I'm not a big fan of redefining base types like that.

Assuming you haven't already have you looked into using std::string
It would provide the functionality and type-safety you want.

It also seems like you have some code quality issues if you have to
generate warnings for incorrect function calls.  Either you should be
able to rely on passing valid parameters to some extent or you need to
fix the parameter types.  Is there a big reason to make the parameter
char* instead of const char*?  Any string literal will be const!

corey

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-08-23  2:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-22  1:49 Looking for a warning when casting 'const char *' to bool Andrew Frezell
2008-08-22 11:22 ` Eljay Love-Jensen
2008-08-23  2:02   ` corey taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).