Function returning a lambda expression

后端 未结 6 1882
猫巷女王i
猫巷女王i 2020-11-29 18:24

I wonder if it\'s possible to write a function that returns a lambda function in C++11. Of course one problem is how to declare such function. Each lambda has a type, but th

6条回答
  •  攒了一身酷
    2020-11-29 19:00

    You should write like this:

    auto returnFunction = [](int x){
        return [&x](){
            return x;
        }();
    };
    

    to get your return as a function, and use it like:

    int val = returnFunction(someNumber);
    

提交回复
热议问题