Why is the gcc option "-Wstrict-prototypes" not suitable for C ++?

Here is a warning from me and many people on the Internet, see when running gcc in C ++ code:

cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++ 

The warning text is very clear: "C ++" is not included in the [Ada / C / ObjC] set, so I have no questions about why gcc gives this warning when compiling C ++ code. (FYI is the reason why we have this flag, despite the presence of C ++ code, because it is mainly C code, we chose a strict (high level) list of warnings, but we added some C ++ code.

My question is: why is this warning not valid for C ++?

The gcc documentation for the warning option from http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Warning-Options.html :

-Wstrict-prototypes (C and Objective-C only) Warn if a function is declared or defined without specifying argument types. (An definition of an old-style function is permitted without warning if preceded by an declaration defining argument types.)

Now I just know that I am forgetting something obvious about C ++, but does not C ++ also require specifying argument types for functions in the prototype? True, these function prototypes are often found in class declarations because functions are often member functions, but aren't prototypes required? Or even if it's just good practice, why not gcc offer this option? Or, if not, a parallel option for C ++?

+6
source share
4 answers

I suppose this is because C ++ requires strict prototypes as part of the language, so the option is superfluous. Why does this make the GCC complain that it is outside of me.

I have this option installed in my build script for small programs with C / C ++ sample / test, and the annoying look annoys me - there seems to be no reason to warn, because the default behavior for the language is what I ask . But it is there, so one fine day, when it annoys me enough, I will correct my script so as not to worry about this parameter for building C ++.

+8
source

It is implicit in C ++, since declaring / defining a function without specifying argument types is illegal by standard C ++ (yes, this is one of the differences between C and C ++, which makes C ++ not a true superset).

This is legal C99, but not legal C ++ 03:

 void foo(x, y) int x; char *y; { // ... } 

GCC gives a warning for this in C if compiled with -Wstrict-prototypes .

+3
source

It is required by the C ++ standard, so it makes no sense to turn it on or off: it is always turned on in the language.

+3
source

Another interesting special case:

 extern int foo(); 

In C semantics, this declaration indicates an incomplete type for foo, where the number and type of arguments remain undefined. This is completely legal on C99 / C11, but -Wstrict-prototypes gives a warning for this declaration in C.

In C ++ semantics, this declaration indicates the type complete for foo, as a function that takes no arguments (i.e. is equivalent to extern int foo(void) ). Therefore, -Wstrict-prototypes does not apply to this case in C ++.

0
source

Source: https://habr.com/ru/post/892792/


All Articles