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

Ville Voutilainen ville.voutilainen at gmail.com
Wed Jan 22 23:51:27 CET 2014


On 22 January 2014 20:29, Richard Smith <richardsmith at google.com> wrote:
> 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.)

Ok.

>> 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.

Agreed.

>> > 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.

It seems that gcc still allows this lovely piece of shenanigans:

int main() {int y = sizeof(decltype(main)*);}


Should this be a core issue? Apparently there has been, and still is,
implementation divergence.


More information about the ub mailing list