Boost C++ Libraries: Ticket #4820: any swaps inefficiently https://svn.boost.org/trac10/ticket/4820 <p> Apologies if this isn't a bug - I am quite new to boost, however I checked in the IRC channel and it doesn't seem like this is a total non-starter. </p> <p> It seems that if you simply call swap, std::swap is called, resulting in an inefficient swapping operation. For example: </p> <p> Big b1, b2; any a1=b1, a2=b2; </p> <p> swap(a1,a2); <em>Calls std::swap, invokes three copy constructors </em></p> <p> I've attached a demonstration file, which I compiled with gcc. </p> <p> Seems like can be easily solved with an overloaded std::swap, but I know little about boost, so I'll leave that for the experts :) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4820 Trac 1.4.3 jonathandodd@… Sun, 07 Nov 2010 02:27:45 GMT attachment set https://svn.boost.org/trac10/ticket/4820 https://svn.boost.org/trac10/ticket/4820 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cc</span> </li> </ul> <p> demo of any::swap </p> Ticket Bryce Adelstein Lelbach Sun, 07 Nov 2010 06:08:11 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4820#comment:1 https://svn.boost.org/trac10/ticket/4820#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> As I explained on IRC, this is not inefficient. Two copy ctor calls happen in the construction of the boost::any objects. </p> Ticket jonathandodd@… Sun, 07 Nov 2010 16:32:03 GMT <link>https://svn.boost.org/trac10/ticket/4820#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4820#comment:2</guid> <description> <p> To clarify, when I run this there are in fact 5 copy constructor calls, 3 of which were generated by swap. </p> <p> I have been assured that this is just me not turning on compiler optimisations, or not using boost::swap. However, even with the -o3 switch in gcc, and even with boost::swap, I still get 3 copy constructor calls. Since the best thing to do is just to swap over the pointers, I expected none. </p> </description> <category>Ticket</category> </item> </channel> </rss>