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 ()
).
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.
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.
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.
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.
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.
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. [..]
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With