This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Warning C vs C++
- From: Ian Lance Taylor <ian at airs dot com>
- To: Vassili Karpov <vassili dot karpov at iss dot ru>
- Cc: gcc at gcc dot gnu dot org
- Date: 17 Sep 2005 06:16:51 -0700
- Subject: Re: Warning C vs C++
- References: <200509171613.16207.vassili.karpov@iss.ru>
Vassili Karpov <vassili.karpov@iss.ru> writes:
Since this e-mail is not about gcc development, it should have been
sent to gcc-help@gcc.gnu.org, not to gcc@gcc.gnu.org. Thanks.
> #include <string.h>
>
> int main (int argc, char *argv[])
> {
> char *s1 = argv[0];
> char *s2 = strchr (s1, '/');
> char r;
>
> (void) argc;
>
> r = s2 ? (s2 - s1) : strlen (s1);
> return 0;
> }
> a. Why it does not err on just -Wall?
The warning is controlled by -Wsign-compare, which is turned on by
-Wextra (also known as -W) but not by -Wall. It's not turned on by
-Wall because it is not normally a problem.
> b. Why is error message with -W what it is? instead of something like
> what microsoft's compiler produces:
>
> cl /Wall /c cvscxx.c
> cvscxx.c(11) : warning C4244: '=' : conversion from 'size_t' to 'unsigned
> char', possible loss of data
As far as I can tell, gcc does not warn about narrowing assignments.
I don't think people would object to adding such a warning, though
probably not under -Wall. If you want to make an enhancement request,
please file it at http://gcc.gnu.org/bugzilla/. Thanks.
> c. Why when compiled with C++ compiler there is no warning at all even with
> -W (or even -Wextra)?
The C and C++ frontends are different. The C++ frontend doesn't
happen to have this particular warning. In othe words, there is no
good reason. Again I don't think people would object to adding the
warning to C++.
> d. What is actual type of the ?: expression? ptrdiff_t? size_t?
The actual type is size_t, because in C, in an expression which uses
both a signed type and an unsigned type of the same size, the signed
type is converted to the unsigned type.
Ian