This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH RFA: Support -Wmissing-declarations in C++


Gabriel Dos Reis <gdr@cs.tamu.edu> writes:

> Ian Lance Taylor <iant@google.com> writes:
> 
> | Currently -Wmissing-declarations only works for C.  But it makes just
> | as much sense for C++ as it does for C.  This patch adds support for
> | -Wmissing-declarations in C++.
> 
> I don't think it makes that much sense for C++, let alone as much sense.
> 
> In C++, if you don't have a declaration in scope, it is a hard error.
> In C, the situation is different, which led to -Wmissing-declarations.
> 
> -Wmissing-declarations embodies C ways of viewing and programming,
> that are counter-idiomatic in C++.
> 
> Furthermore, a function can be declared only once at class scope.  The
> artificial dissymmetry that this switch will introduce for such a
> fundamental notion as class in C++ is a strong hint that this switch
> is best left for the C front-end.

I disagree.  It makes sense in the C++ world too, though I agree that
it is not as important in C.  This option applies only to global
functions, not to classes, nor to functions declared within classes
(aka methods).

You have misstated the reason for the warning.  I actually suggested
this option originally, for C, back in 1990 or so.  The case this
warning is for is:

foo.h:
  extern int foo();
bar.cc:
  #include "foo.h"
  int bar() { return foo(); }
quux.cc:
  double foo() { return 0; }

In other words: quux.cc forgot to include "foo.h", and then somebody
changed the definition of foo() without updating the declaration in
"foo.h".

This does not happen as often in C++ as it does in C, but it can and
does happen.  And it did in fact happen in some C++ code here, which
is why I thought to make the option work for C++.

Note that this option is not turned on by -Wall.  It is entirely
optional.  Since it makes just as much sense for C++ as for C, I see
no reason not to enable it for C++.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]