C++ Logo

sg12

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 19:14:30 -0800
On 22 January 2014 18:00, David Krauss <david_work_at_[hidden]> wrote:

>
> On Jan 23, 2014, at 2:29 AM, Richard Smith <richardsmith_at_[hidden]>
> wrote:
>
> 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 looks like Clang and GCC reject unevaluated call expressions and unary
> & expressions, but other non-ODR uses go undetected.
>
> (void) main; // no diagnosis
> (void) & main; // diagnosis
>
> Also, this ODR use which avoids function pointer conversion gets no
> diagnosis from either.
>
> template< int (&)() >
> struct s {};
> s< main > q; // no diagnosis
>
> GCC diagnoses this analogous case, but Clang does not; it happily
> generates the recursive program.
>
> #include <iostream>
>
> void f( int (&p)() ) { p(); }
>
> int main() {
> std::cout << "hello\n";
> f( main ); // GCC diagnosis, not Clang.
> }
>
>
> For the record, the GCC manual says that -pedantic and -pedantic-errors
> are required to get ISO-required diagnoses that aren’t “gratuitous.” So it
> seems there’s no sense worrying what they allow in non-pedantic mode.
>

Up until recently, Clang did not enforce this rule. In -pedantic-errors
mode, Clang trunk rejects all of your above examples.

Received on 2014-01-23 04:14:33