Destructor
Destructor

Reputation: 14438

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

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?

Upvotes: 2

Views: 311

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283684

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.

Upvotes: 6

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

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.

Upvotes: 2

Related Questions