简体   繁体   中英

Why, if the c++ standard says that the syntax is incorrect, does g++ allow it?

I just read a comment that said something along the likes of:

"You should never use void main() you should always use int main() ."

Now I know the reasons for using int main() (so that you can check for success on return and whatnot) but I didn't know that using void main() was illegal. I did some investigating and the only reason I could find not use void main() is because the "standard says so".

My question is: Why, if the C++ standard says that main must return a value, does g++ allow programmers to use void main() as valid syntax? Shouldn't it return an error / warning because it goes against what the standard says?

That only means that a particular version of your compiler may allow it, but the later versions (which is likely to be more Standard conformant) may not allow it. So better write Standard Conformant code from the beginning!

According to the standard, main is indeed required to return int. But many compilers allow a return type of void since in pre-standard C++ it was allowed, and for a long time much code was written with a return type of void.

It is also worth to mention that C++ explicitly allows obission of the return statement for void:

int main() {
}

will return 0. But that is only allowed for main.

The GNU Project has a decent summary of their philosophy :

In most cases, following published standards is convenient for users—it means that their programs or scripts will work more portably. ...

But we do not follow either of these specifications rigidly, and there are specific points on which we decided not to follow them, so as to make the GNU system better for users.

For instance, Standard C says that nearly all extensions to C are prohibited. How silly! GCC implements many extensions, some of which were later adopted as part of the standard. If you want these constructs to give an error message as "required" by the standard, you must specify --pedantic , which was implemented only so that we can say "GCC is a 100% implementation of the standard," not because there is any reason to actually use it.

POSIX.2 specifies that df and du must output sizes by default in units of 512 bytes. What users want is units of 1k, so that is what we do by default. If you want the ridiculous behavior "required" by POSIX, you must set the environment variable POSIXLY_CORRECT (which was originally going to be named POSIX_ME_HARDER ). ...

In particular, don't reject a new feature, or remove an old one, merely because a standard says it is "forbidden" or "deprecated."

Sometimes, GCC has removed extensions when they caused confusion like this one. I believe this extension existed to allow old code with an incorrect main declaration to compile, not necessarily to encourage people writing void main() . Similar to the extension that allowed pre-POSIX function declarations. Besides, while int main(int argc, const char** argv) is the C-approved declaration for main , the C++ standard also sanctions int main() , and POSIX sanctions int main(int argc, const char** argv, const char** envp) . There may well be other declarations that I haven't run into yet.

You can force the compiler to be standards compliment by using the following the build commands:

-ansi -pedantic -Wall

If you are not coding cross-platform code then -c99 might be a better choice. Not all compilers support that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM