+ 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

14th Dec 2023, 1:31 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Antworten
+ 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
14th Dec 2023, 4:18 PM
MO ELomari
MO ELomari - avatar
+ 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.
14th Dec 2023, 2:44 PM
Bob_Li
Bob_Li - avatar
+ 3
Great and thank you MO ELomari
14th Dec 2023, 4:22 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
Bob_Li just for practicing and discovering what we're allowed to do and when and what we don't .
14th Dec 2023, 4:57 PM
MO ELomari
MO ELomari - avatar
+ 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;};
14th Dec 2023, 4:35 PM
Bob_Li
Bob_Li - avatar
+ 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.
14th Dec 2023, 9:14 PM
Bob_Li
Bob_Li - avatar