C++ Logo

SG12

Advanced search

Subject: Re: [ub] What does "The function main shall not be used within a program" mean?
From: David Krauss (david_work_at_[hidden])
Date: 2014-01-21 19:40:35


On Jan 22, 2014, at 9:11 AM, Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:

> On 22 January 2014 02:55, David Krauss <david_work_at_[hidden]> wrote:
>> 1. Violation of “shall” is diagnosable unless otherwise specified (1.4/1).
>
> Ok. I don't think this is obviously clear.

No? It’s the usual blanket rule. Not clear if that’s how compliance works, or if it applies in this instance?

>> 2. I would take “use” to mean ODR-use.
>
> I wouldn't, I would take "use" to mean any use, not just odr-use.

Well, a definition is also a use, in the sense that the name is looked up. Also the linkage is implementation-defined, while if no aspect could ever be inspected, it might as well be merely implementation-specific.

I don’t see what utility other uses could have though, given that ODR-use isn’t allowed.

The language of 3.6.1/2 seems to imply that main may have a type different from its declared type, so decltype and auto would give incorrect results. This makes sense in the context of allowing the user to ignore argc and argv. (I wonder how C deals with this.) Most implementations quietly adjust the language linkage at least.

>> 3. Far as I know, the implementation is allowed to perform dynamic initialization of globals in main. So for all intents and purposes, it’s not really usable as a function because its body doesn’t describe what it does.
>
> Well, apparently some implementations think they can get away with it
> under the auspices of undefined behavior. :)

Eh… each implementation is ultimately written for the satisfaction of its own customers, and there are enough folks who like to recurse main.

I don’t recall ever seeing an implementation not to allow safe recursion of main. Often the C runtime implementation already performs atexit(), OS service initialization, and maybe memory initialization. So even without C++, main is already a client function within the runtime. As long as the platform has separate C and C++ runtime libraries, problems are unlikely. If it puts all of C++ atop a wrapper around C (a la Cfront), only then does main become a tempting place to construct globals.


SG12 list run by herb.sutter at gmail.com