2

I'm trying to make the initialization of a lambda expression in class. The pseudo code could be like this

class A{
  //stuff..
  static constexpr auto lambda = [] (unsigned char element){//stuff..};

};

When i tried to compile i get this error message

 error: ‘constexpr const A::<lambda(unsigned char)> A::get_range’, declared using local type ‘const A::<lambda(unsigned char)>’, is used but never defined [-fpermissive]
 static constexpr auto lambda = [](unsigned char element){

How can i do this in class initialization? Thank you indeed and sorry for the English.

Peter Tovar
  • 138
  • 5
P.Carlino
  • 661
  • 5
  • 21

1 Answers1

0

As of C++17, lambdas can be constexpr, (P0170R1) this is however not supported on all compilers yet, so depending on your compiler, it might or might not work (g++ supports this since version 7, the intel compiler will feature it from 19.0 on).

As a workaround, you can use decltype to implement a static const lambda member

auto lambda = [](unsigned char element){};
class A{
   //stuff
   static const decltype(lambda) a;
};
const decltype(lambda) A::a{lambda};
Kai Guther
  • 783
  • 3
  • 11