What does it mean that linkage of main() is implementation defined?

我与影子孤独终老i 提交于 2021-02-07 09:49:03

问题


C++ standard section 3.6.1/3 says that

The linkage of main is implementation-defined

What does it mean? Why it is implementation defined? Is it same in C also?


回答1:


The purpose of C++ is to provide a portable abstraction over programming. Many things are specified by the standard so as to be unambiguous regardless of whether you translate your C++ to assembly, JavaScript, cheese, frying pans or supermodels.

The linkage of main is not one of those things, because it is a bit of an abstraction leak: it is (theoretically) the function that interacts with the pieces of the executing machine/cheese/frying pan and handles data crossing that boundary. Data in, data out.

Substantial details about the main function should not be standard-mandated because the entire purpose of main is to interface with things that the standard cannot control.

That being said, there are still significant restrictions emplaced upon main, and in most implementations it's not even used as the entrypoint — some internal function in your compiler's C++ runtime will usually act as the the entrypoint, performing static initialisation and a few other things before invoking main, because, well, that's about the only sane way to do it.




回答2:


Because references to the function main are forbidden (it helps if you quote the entire rule), linkage of main has absolutely no effect on user code:

The function main shall not be used within a program. The linkage of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill-formed. The name main is not otherwise reserved.

Linkage controls the scope across which the name is usable, the name of the main() function isn't usable by your code anywhere at all, so trying to label it with a linkage doesn't make sense.



来源:https://stackoverflow.com/questions/31222601/what-does-it-mean-that-linkage-of-main-is-implementation-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!