C++ Logo


Advanced search

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

From: Stephen Clamage <stephen.clamage_at_[hidden]>
Date: Tue, 21 Jan 2014 17:08:30 -0800
Historically, function main in C++ often needed to perform some magic,
and the actual implementation of main might not behave in the same way
as an ordinary function with the same definition. If you could declare
or call main, or take its address, results would be unpredictable. Thus,
the restrictive rules allow main to be special, where needed.

If you want a recursive main, or want to *do* something with it, you can
write mymain as an ordinary function, and have the real main just call
it. Example, for the case of using only argc and argv:

int mymain(int argc, char **argv)
     // do whatever you want with mymain

int main(int argc, char **argv)
     return mymain(argc, argv);

So the restrictions don't cause any real loss of functionality.

- Steve Clamage

On 01/21/14 16:55, David Krauss wrote:
> 1. Violation of “shall” is diagnosable unless otherwise specified (1.4/1).
> 2. I would take “use” to mean ODR-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.
> On Jan 22, 2014, at 8:49 AM, Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:
>> An infinite recursion example:
>> int main() {auto* x = &main; x();}
>> A non-infinite one:
>> int x = 0; int main() {auto* f = &main; if (++x == 0) f();}
>> gcc rejects these with a diagnostic (when given -pedantic). clang does
>> not.
>> 1) does a violation of a "shall" requirement mean UB in general,
>> or ill-formed (if ill-formed, is a diagnostic required?)?
>> 2) what does it mean in this particular case?
>> 3) why?
>> _______________________________________________
>> ub mailing list
>> ub_at_[hidden]
>> http://www.open-std.org/mailman/listinfo/ub
> _______________________________________________
> ub mailing list
> ub_at_[hidden]
> http://www.open-std.org/mailman/listinfo/ub

Received on 2014-01-22 02:51:44