Boost: When the bind constructor object is used, the real parameter transmission adopts the value passing method, and will be constructed several times in the middle. If the copy construction is not allowed or the copy construction has a high performance overhead, you can use boost: ref to add a wrapper layer to avoid copy construction. The following is the sample code:
# Include <iostream>
# Include "boost/bind. hpp"
# Include "boost/ref. hpp"
Struct
{
A (int aValue): m_value (aValue)
{
Std: cout <"A ()" <std: endl;
}
A (const A &)
{
Std: cout <"A (const A &)" <std: endl;
This-> m_value = a. m_value;
}
Int m_value;
};
Void f (A)
{
Std: cout <"f (A)" <std: endl;
}
Void g (A &)
{
Std: cout <"g (A &)" <std: endl;
}
Int main ()
{
A a (10 );
Std: cout <"... f1...." <std: endl;
Boost: bind (& f, )();
Std: cout <"... f2...." <std: endl;
Boost: bind (& f, boost: ref ())();
Std: cout <"... g1...." <std: endl;
Boost: bind (& g, )();
Std: cout <"... g2...." <std: endl;
Boost: bind (& g, boost: ref ())();
Return 0;
}
Output:
A ()
... F1...
A (const &)
A (const &)
A (const &)
A (const &)
A (const &)
A (const &)
F ()
... F2...
A (const &)
F ()
... G1...
A (const &)
A (const &)
A (const &)
A (const &)
A (const &)
G (&)
... G2...
G (&)