+ 1
What's the error In named lambda inside class
Hi Refer code below : It is working fine as per expectation. I just created named lambda called otherlambda in main function and then called same. When I am doing so inside class , I got the error. Yo get the error, just uncomment the code related to mylambda. How to get these resolved ? https://sololearn.com/compiler-playground/cNu9O1lFln2I/?ref=app
6 Respuestas
+ 4
a placeholder type specifier (i.e. auto, decltype(auto) (since c++14) , a class template name subject to deduction (since C++17), a constrained placeholder (since C++20) ) cannot be used in a non-static data member declaration. (although it is allowed for static data members that are initialized in the class definition ).
in order to achieve you goal You could use < std::function > :
https://code.sololearn.com/cx13LnCU7d2y/?ref=app
https://www.sololearn.com/en/compiler-playground/cx13LnCU7d2y
+ 4
don't use it?
The only way I got mylambda to work was if m_x was static const and mylambda was static constexpr. But that will make m_x useless...
lambda is just too restrictive. It have it's use case, but maybe using a regular function is better in this case.
+ 3
Great and thank you MO ELomari
+ 3
Bob_Li
just for practicing and discovering what we're allowed to do and when and what we don't .
+ 2
MO ELomari
nice.
But doesn't this makes using a lambda not really easier than simply using a regular function? I don't see any advantage in doing it this way, other than extra complication.
void mylambda(){ std::cout << m_x << std::endl;};
+ 1
MO ELomari
right. It is good to know.
C++ 's bag of tricks is a very big one indeed.😅
But sometimes, the saying "Just because we could, does not mean we should" is a good one to remember.
It is very easy to make the code more complicated than it needs to be.