C++ Logo


Advanced search

Re: [ub] What does "The function main shall not be used within a program" mean?

From: Richard Smith <richardsmith_at_[hidden]>
Date: Wed, 22 Jan 2014 10:29:11 -0800
On 21 January 2014 22:12, Ville Voutilainen <ville.voutilainen_at_[hidden]>wrote:

> On 22 January 2014 03:40, 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?
> Both.

1.4/2, bullet 2, seems clear to me: this is a diagnosable rule, so a
conforming implementation must issue at least one diagnostic for a program
that violates it. (After doing so, if the implementation accepts it anyway,
the behavior of the program is undefined, as for any diagnosable rule.)

> >>> 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.
> I don't think decltype(main()) is an odr-use, or sizeof(decltype(main)).

C++98's mention of 'use' had a cross-reference to 3.2 [basic.def.odr].
C++11 no longer has the cross-reference, and was *not* changed to say
'odr-use', so I expect it means *any* use.

> >>> 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.
> Really?
> > I don’t recall ever seeing an implementation not to allow safe recursion
> of main. Often the C runtime
> gcc doesn't allow it if given -pedantic.

Clang trunk also doesn't allow it with -pedantic-errors. Both GCC and Clang
reject decltype(main()) too, in strictly-conforming mode. EDG rejects
odr-uses of main but accepts uses that are not odr-uses.

Received on 2014-01-22 19:29:13