Note that there are some explanatory texts on larger screens.

plurals
  1. POExtra copy constructors when std::function is used
    primarykey
    data
    text
    <p>In my project, there are two components: producer and consumer. Producer is responsible for processing some information and giving the result to consumer. The result is passed with help of function object. </p> <p>I am using a function for passing this information. You could see how it could look in the code below. </p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;functional&gt; using namespace std; class Data { std::vector&lt;int&gt; vec; public: Data() { cout &lt;&lt; "Data()" &lt;&lt; endl; } Data(const Data&amp; rr) : vec(rr.vec) { cout &lt;&lt; "Data(Data&amp;)" &lt;&lt; endl; } ~Data() { cout &lt;&lt; "~Data()" &lt;&lt; endl; } Data(Data&amp;&amp; rr) : vec(move(rr.vec)) { cout &lt;&lt; "Data(Data&amp;&amp;)" &lt;&lt; endl; } void get() { } }; class Producer { public: void process(function&lt;void(Data)&gt; f) { Data data; f(move(data)); } void process2(void(&amp;pf)(Data)) { Data c; pf(move(c)); } }; void Consume(Data a) { cout &lt;&lt; "Consume(data)" &lt;&lt; endl; } int main() { { cout &lt;&lt; "use function() " &lt;&lt; endl; Producer p; p.process([](Data a) { cout &lt;&lt; "Consume(data)" &lt;&lt; endl; }); } { cout &lt;&lt; endl; cout &lt;&lt; "use function pointer" &lt;&lt; endl; Producer p;; p.process2(Consume); } return 0; } </code></pre> <p>it has the following output</p> <pre><code> use function() Data() Data(Data&amp;&amp;) Data(Data&amp;) Data(Data&amp;) Consume(data) ~Data() ~Data() ~Data() ~Data() use function pointer Data() Data(Data&amp;&amp;) Consume(data) ~Data() ~Data() </code></pre> <p>There are additional copy constructors when function object is used. </p> <p>Did I do something wrong? Is it possible to get rid of these extra constructors? </p> <p>Thank you in advance.</p> <p>I am using VC10 SP1.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload