Here is the sample code
class A
{
public:
int a{ 3 };
A() { std::cout << "con\n"; };
A(const A&) { std::cout << "con2\n"; };
};
int main()
{
std::function<void(A)> f = [](A a) {};
A data;
auto p = std::bind(f,data);
p();
return 0;
}
Here is the output
con
con2
con2
con2
I knew that the cause of the first three line output
- create the data object
- capture it as a member of object p
- pass the A object by value
but why it calls the constructor again?
BTW, the g++ and vc++ share the same behavior
Aucun commentaire:
Enregistrer un commentaire