Following up my previous question some months ago, I would like to ask how a container internally handle the lampda expression.
Assuming following piece of code:
#include <iostream>
#include <type_traits>
#include <queue>
#include <functional>
std::queue<std::function<void()>> funcs;
class A
{
public:
A()
{
std::cout << "A constructor" << std::endl;
}
void foo(int a)
{
}
};
class ThePool
{
public:
template <typename _Callable, typename Object, typename... _Args>
void QueueFunction(_Callable __f, Object& obj, _Args... __args)
{
funcs.push([__f, &obj, &__args...]() mutable
{
(obj.*__f)(__args...);
});
}
};
int main(int argc, char** argv)
{
ThePool t;
A a;
int x = 5;
t.QueueFunction(&A::foo, a, x);
std::function<void()> func = funcs.back();
func();
return 0;
}
If pass an instance of class A and args as value it is clear that the values are copied. I confuse what happens if I pass the instance of class A as reference and args as reference. I know that internally in a container copy takes place. From my point view the address of the function pointer will be copied. Are the instance of the obj and args be copied as well?
Doing an example I notice that when I passed the A instance as reference and finally extract the function f for the queue and call it the instance of A and this are not the same.