#define WIRED\
[]() {\
return new MyClass; \
}()
MyClass* a = WIRED;
I don't understand the first []()
and the last ()
in the MACRO. Can any expert explain this? Thanks.
#define WIRED\
[]() {\
return new MyClass; \
}()
MyClass* a = WIRED;
I don't understand the first []()
and the last ()
in the MACRO. Can any expert explain this? Thanks.
It's a lambda returning a dynamically allocate MyClass
that is created and executed in place.
Specifically:
[](){...}
is the lambda; and the remaining ()
is the invocation of the lambda.
It's a useless lambda AFAIK and you can simply write:
MyClass* a = new MyClass;
or better:
std::unique_ptr<MyClass> ptr(new MyClass);
You may need to understand lambda function from C++11.
I believe you can understand this:
MyClass* CreateMyClass() { return new MyClass; }
...
std::function<MyClass* ()> creator_function = CreateMyClass;
MyClass* a = creator_function();
Then, lambda function seems like:
std::function<MyClass* ()> creator_function = []() { return new MyClass; };
MyClass* a = creator_function();
Finally, this would also work:
MyClass* a = []() { return new MyClass; }();