C++ Logo


Advanced search

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

From: David Krauss <david_work_at_[hidden]>
Date: Thu, 23 Jan 2014 10:00:15 +0800
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.

Received on 2014-01-23 03:00:43