[cfe-dev] Calling main() is ub based optimizations

Ben Taylor via cfe-dev cfe-dev at lists.llvm.org
Mon Nov 14 02:58:41 PST 2016


The relevant piece of standardese seems to be 3.6.1.3 in the C++17 working
draft (n4606) standard, which reads in part:

"The function main shall not be used within a program. The linkage (3.5) of
main is implementation-defined. A program that defines main as deleted or
that declares main to be inline, static, or constexpr is ill-formed. The
main function shall not be declared with a linkage-specification (7.5). A
program that declares a variable main at global scope or that declares the
name main with C language linkage (in any namespace) is ill-formed."

So the cases you suggest, by my reading, should not be UB but rather a
compile error.

--Ben

On Mon, Nov 14, 2016 at 5:48 AM, David Chisnall via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> On 14 Nov 2016, at 09:43, Eric Fiselier via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
> >
> >
> >
> > On Sat, Nov 12, 2016 at 11:09 AM, Mehdi Amini via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
> > main is marked as “norecurse” in C++ and not in C:
> >
> > I would be interested in seeing the standardese associated with each
> behavior.
> >
> > /Eric
>
> Indeed.  If I implement main in C++ and call it from C, or implement main
> in C and call it from C++, is it undefined behaviour in both cases?  This
> seems like a very odd inconsistency if I have to know the source language
> for both the caller and callee to determine whether something is UB.
>
> David
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161114/69b5ace6/attachment.html>


More information about the cfe-dev mailing list