Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the main (or entry-point) function be implemented as a lambda?

Is this valid under the recently updated standard?

auto main = [](int argc, char* argv[]) -> int
{
    return 0;
};

My best guess is that it depends on whether main() MUST be a function, or if it is allowed to be any globally scoped symbol that is callable (with ()).

like image 548
Michael Price Avatar asked Oct 21 '11 15:10

Michael Price


People also ask

How are lambdas implemented in C++?

C++ provides a convenient mechanism for achieving this with lambdas called 'capturing the context'. The context of a lambda is the set of objects that are in scope when the lambda is called. The context objects may be captured then used as part of the lambda's processing.

What is lambda function?

A Lambda Function , or a Small Anonymous Function , is a self-contained block of functionality that can be passed around and used in your code. Lambda has different names in different programming languages – Lambda in Python and Kotlin, Closure in Swift, or Block in C and Objective-C.

Are lambda functions faster C++?

sorting - Why is a C++ Lambda function much faster as compare function than an equivalent object - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is a lambda callback?

The third argument, callback , is a function that you can call in non-async handlers to send a response. The callback function takes two arguments: an Error and a response. When you call it, Lambda waits for the event loop to be empty and then returns the response or error to the invoker.


3 Answers

No, main is required to be a global function and cannot be a function object or anything else. See ISO/IEC 14882:2011 § 3.6.1 Main Function.

A program shall contain a global function called main, which is the designated start of the program.

And from paragraph 2

All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

There is no requirement for implementations to allow any other definitions.

like image 131
bames53 Avatar answered Oct 06 '22 04:10

bames53


No, and here's why:

[n3290: 3.6.1/1]: A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]

Lambdas are not functions, but function objects or functors:

[n3290: 5.1.2/3]: The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. [..]

like image 41
Lightness Races in Orbit Avatar answered Oct 06 '22 04:10

Lightness Races in Orbit


main() must be a function because of the way it's called from within with the system libraries . It is part of the POSIX.1 standard and governs the way C linkage works

The main linkage has to be an extern global, it cannot be inlined or made static because it's called from within the libc and typically from a function called _start.

As an example, typical implementation of _start in glibc is:

int _start() {
     __libc_init(argc, argv, __environ);
     exit(main(argc, argv, __environ));
}

Various libc implementations will do it in a similar fashion.

In C++ the main function must be declared in the global scope (i.e.) ::main(); again because it is called from an init-like function such as _start for libc on *nix function above after execution...

like image 36
Ahmed Masud Avatar answered Oct 06 '22 05:10

Ahmed Masud