0

I'm trying to initialize templated class member with a lambda, via the constructor, like this.

#include <functional>

template<typename T>
class lambda_template_class {
public:
    lambda_template_class(std::function<T(T)>& f) { }
};

class test {
protected:
    lambda_template_class<int> member{ [] (int x) { return x; } };
};

int main() {
    test t;
}

But it does not seem to accept any syntax. Is there a simple way to make this work?

Tomaz Stih
  • 529
  • 3
  • 10
  • 3
    `lambda_template_class(std::function& f)` -> `lambda_template_class(std::function f)` – Yksisarvinen Oct 19 '20 at 19:19
  • Thank you, this solves the problem. – Tomaz Stih Oct 19 '20 at 19:20
  • Just a note, your question can reproduced simpler as `void foo(int&){} foo(5);`, it has nothing to do with functions or lambdas. It's just the problem of passing a temporary by reference (and I do hope you don't have any member variables as references, it won't work). – Yksisarvinen Oct 19 '20 at 19:22
  • I can't mark comments as solution, but you did solve the problem. Can you post an answer which I can mark as a solution? – Tomaz Stih Oct 19 '20 at 19:22

0 Answers0