Is it possible to replace one std::function
from within itself with another std::function
?
The following code does not compile:
#include <iostream>
#include <functional>
int main()
{
std::function<void()> func = []()
{
std::cout << "a\n";
*this = std::move([]() { std::cout << "b\n"; });
};
func();
func();
func();
}
Can it be modified to compile?
The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could capture func
's this
-pointer. I guess, it is not even a std::function
inside the lambda, yet?! How can this be done?
Background: What I want to achieve is the following: In the first invocation of a given std::function
, i would like do some initialization work and then replace the original function with an optimized one. I want to achieve this transparently for the user of my function.
The expected output of the example above is:
a
b
b
Aucun commentaire:
Enregistrer un commentaire