Boost C++ Libraries: Ticket #8674: Futures as local named objects can't be returned with implicit move. https://svn.boost.org/trac10/ticket/8674 <p> I could test only with VS 2012 Update 2 so this might be related to a compiler bug, but I can't test the boost code with other compilers/platforms right now. </p> <p> I have this simple test: </p> <pre class="wiki">#include &lt;iostream&gt; #define USE_STD 0 #define USE_BOOST 1 #define USED_THREAD_API USE_BOOST #if USED_THREAD_API == USE_BOOST # define BOOST_THREAD_VERSION 4 # include &lt;boost/thread/future.hpp&gt; using boost::future; using boost::async; #endif #if USED_THREAD_API == USE_STD # include &lt;future&gt; using std::future; using std::async; #endif future&lt;void&gt; do_something() { auto result = async( []{ std::cout&lt;&lt; "A\n"; } ); std::cout &lt;&lt; "B\n"; return result; // error here } int main() { do_something().wait(); std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl; } </pre><p> Both default Debug and Release modes fail to compile on VS2012 U2: </p> <pre class="wiki"> 1&gt;------ Build started: Project: Test_MoveReturnFuture, Configuration: Debug Win32 ------ 1&gt; main.cpp 1&gt;e:\projects\tests\test_movereturnfuture\test_movereturnfuture\main.cpp(29): error C2248: 'boost::future&lt;R&gt;::future' : cannot access private member declared in class 'boost::future&lt;R&gt;' 1&gt; with 1&gt; [ 1&gt; R=void 1&gt; ] 1&gt; e:\projects\sdk\boost\boost\include\boost-1_53\boost\thread\future.hpp(1406) : see declaration of 'boost::future&lt;R&gt;::future' 1&gt; with 1&gt; [ 1&gt; R=void 1&gt; ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== </pre><p> Changing this line: </p> <pre class="wiki">#define USED_THREAD_API USE_BOOST </pre><p> To this: </p> <pre class="wiki">#define USED_THREAD_API USE_STD </pre><p> Makes it compile and execute fine. </p> <p> I see that there are move constructors in the definition of future so I don't know at all what's the difference between the two implementations. </p> <p> Another way to avoid the problem is to explicitly move the future: </p> <pre class="wiki">future&lt;void&gt; do_something() { auto result = async( []{ std::cout&lt;&lt; "A\n"; } ); std::cout &lt;&lt; "B\n"; return std::move(result); } </pre><p> Which is what I have been doing in my code because I was initially thinking it was a compiler bug, but this Q/A made me reconsider: <a class="ext-link" href="http://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion"><span class="icon">​</span>http://stackoverflow.com/questions/4986673/c11-rvalues-and-move-semantics-confusion</a> </p> <p> I cannot test Boost 1.54 at the moment. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8674 Trac 1.4.3 viboes Sat, 08 Jun 2013 16:05:33 GMT owner, status changed https://svn.boost.org/trac10/ticket/8674#comment:1 https://svn.boost.org/trac10/ticket/8674#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> IMO, the code is correct. I have tested it with gcc-4.7.2/4.8.0, clang-3.2 and it works as expected with both defines USED_THREAD_API USE_BOOST or USED_THREAD_API USE_STD. </p> <p> Could you show the implementation of std::future constructors of the library you are using? </p> Ticket mjklaim@… Sat, 08 Jun 2013 16:33:17 GMT attachment set https://svn.boost.org/trac10/ticket/8674 https://svn.boost.org/trac10/ticket/8674 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">std_future.hpp</span> </li> </ul> <p> VS2012 U2 &lt;future&gt; implementation </p> Ticket mjklaim@… Sat, 08 Jun 2013 16:34:09 GMT <link>https://svn.boost.org/trac10/ticket/8674#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:2</guid> <description> <p> I copy/pasted the &lt;future&gt; file code into the attached one. It's the one provided with VS2012 U2. I don't use the community betas. </p> <p> Note that the problem might be a VS compiler bug that might be fixed in the community beta. If someone can test the same code with the community beta it would help. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 08 Jun 2013 16:50:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8674#comment:1" title="Comment 1">viboes</a>: </p> <blockquote class="citation"> <p> IMO, the code is correct. I have tested it with gcc-4.7.2/4.8.0, clang-3.2 and it works as expected with both defines USED_THREAD_API USE_BOOST or USED_THREAD_API USE_STD. </p> <p> Could you show the implementation of std::future constructors of the library you are using? </p> </blockquote> <p> I have tried with VS 2010, but there is no &lt;future&gt; header. I confirm the bug with VS 2010 and #define USED_THREAD_API USE_BOOST. Unfortunately I've a windows XP and I can not install VS 2012 :( </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sat, 08 Jun 2013 16:57:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:4</guid> <description> <p> Note that I'm using VS2012 Update 2 but there is Update 3 Release Candidate. <a class="ext-link" href="http://support.microsoft.com/kb/2835600"><span class="icon">​</span>The log don't seem to have anything related to this bug</a>. </p> <p> Also, I'm using 64 and 32 bits and have the error whatever the case. </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sat, 08 Jun 2013 21:47:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:5</guid> <description> <p> I asked <a class="ext-link" href="http://www.developpez.net/forums/d1351972/boost/boostfuture-n-est-pas-retournable-via-implicite-move-vs2012-u2/"><span class="icon">​</span>on a french C++ forum</a> if someone could test with VS2012 November CTP1 (which add several c++11 features but can't be used safely in production). </p> <p> Loic Joly reported the same error (which copying here); </p> <pre class="wiki">1&gt;------ Début de la génération*: Projet*: TestFutureCTP (Microsoft Visual C++ Compiler Nov 2012 CTP), Configuration*: Debug Win32 ------ 1&gt; 'Microsoft Visual C++ Compiler Nov 2012 CTP' is for testing purposes only. 1&gt; Source.cpp 1&gt; ****\visual studio 2012\projects\testfuturectp\source.cpp(29): error C2248: 'boost::future&lt;void&gt;::future' : cannot access private member declared in class 'boost::future&lt;void&gt;' 1&gt; d:\boost\boost_1_53_0\boost\thread\future.hpp(1406) : see declaration of 'boost::future&lt;void&gt;::future' </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 09 Jun 2013 10:24:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8674#comment:2" title="Comment 2">mjklaim@…</a>: </p> <blockquote class="citation"> <p> I copy/pasted the &lt;future&gt; file code into the attached one. It's the one provided with VS2012 U2. I don't use the community betas. </p> <p> Note that the problem might be a VS compiler bug that might be fixed in the community beta. If someone can test the same code with the community beta it would help. </p> </blockquote> <p> I wonder if the following constructor is the one that is been used by the compiler. </p> <pre class="wiki">1126 future(const _Mybase&amp; _State) 1127 : _Mybase(_State, true) 1128 { // construct from associated asynchronous state object 1129 } </pre><p> This will clearly be a compiler bug (and indirectly a library bug). </p> <p> Could you try adding this public constructor to Boost.Thread to confirm it </p> <pre class="wiki"> BOOST_THREAD_FUTURE(base_type const&amp; b): base_type(b.future_) { } </pre> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sun, 09 Jun 2013 11:02:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:7</guid> <description> <blockquote class="citation"> <p> Could you try adding this public constructor to Boost.Thread to confirm it </p> </blockquote> <p> If I add this code it don't compile and give the same exact error. But the error points to this line: </p> <pre class="wiki">BOOST_THREAD_MOVABLE_ONLY(BOOST_THREAD_FUTURE) </pre><p> Obviously, removing it (and keeping the modification) makes it compile. </p> <p> I don't know if it works though, I would have to recompile boost.thread to check on runtime. Isn't this modification duplicating the value? </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sun, 09 Jun 2013 11:08:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:8</guid> <description> <p> I checked by debugging step-by-step the std::future behaviour and it does use the move constructor, not the other one. So no, it's not using the base-copy constructor. </p> <p> Maybe boost.thread futres's move-only macro is hiding the move constructor in some ways? </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sun, 09 Jun 2013 11:16:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:9</guid> <description> <p> Ok I found something. In delete.hpp: </p> <pre class="wiki"> /** * BOOST_THREAD_DELETE_COPY_CTOR deletes the copy constructor when the compiler supports it or * makes it private. * * BOOST_THREAD_DELETE_COPY_ASSIGN deletes the copy assignment when the compiler supports it or * makes it private. */ #ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS #define BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \ CLASS(CLASS const&amp;) = delete; \ #define BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS) \ CLASS&amp; operator=(CLASS const&amp;) = delete; #else // BOOST_NO_CXX11_DELETED_FUNCTIONS #define BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \ private: \ CLASS(CLASS&amp;); \ // &lt;--- HERE public: #define BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS) \ private: \ CLASS&amp; operator=(CLASS&amp;); \ // &lt;--- HERE public: #endif // BOOST_NO_CXX11_DELETED_FUNCTIONS </pre><p> Notice that the non-c++11 implementation define private copy constructor and assignation using <strong>non-const</strong> references. </p> <pre class="wiki">#define BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \ private: \ CLASS(CLASS const &amp;); \ // &lt;--- HERE public: #define BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS) \ private: \ CLASS&amp; operator=(CLASS const &amp;); \ // &lt;--- HERE public: </pre><p> This compiles for me. (I just can't run it right now because it requires recompiling boost.thread) </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sun, 09 Jun 2013 11:19:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:10</guid> <description> <p> Just to clarify: that it works without modification of the future class. </p> <p> So basically, these private constructor/assignation were selected as the right match instead of the moving constructor/assignation, just because they had non-const reference. I'm a bit lost on if it's a compiler bug or not. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 09 Jun 2013 12:19:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:11</guid> <description> <p> Thanks for the analysis. I have checked the const reference patch with VS 2010 and it works now. </p> <p> I will run the whole regression test before committing these changes. </p> <p> Thanks again, Vicente </p> </description> <category>Ticket</category> </item> <item> <author>mjklaim@…</author> <pubDate>Sun, 09 Jun 2013 12:20:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8674#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:12</guid> <description> <p> I'm happy to help. :) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 09 Jun 2013 16:58:21 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/8674#comment:13 https://svn.boost.org/trac10/ticket/8674#comment:13 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> Ticket viboes Sun, 09 Jun 2013 21:31:49 GMT <link>https://svn.boost.org/trac10/ticket/8674#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8674#comment:14</guid> <description> <p> Committed in <a class="changeset" href="https://svn.boost.org/trac10/changeset/84718" title="Thread: used cons&amp; in deleted constructors and assignment simulation.">[84718]</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Wed, 12 Jun 2013 21:00:28 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8674#comment:15 https://svn.boost.org/trac10/ticket/8674#comment:15 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Committed revision <a class="changeset" href="https://svn.boost.org/trac10/changeset/84750" title="Thread: merge fixes for #8422, #8458, #8674.">[84750]</a>. </p> Ticket