<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>std::thread and std::vector confusion?</title>
		<link>http://www.allegro.cc/forums/view/615409</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 31 May 2015 23:00:23 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span><span class="k1">void</span> task1<span class="k2">(</span>std::string msg<span class="k2">)</span>
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"task1 says: %s\n"</span>, msg.c_str<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span><span class="k2">}</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k1">void</span>  main<span class="k2">(</span><span class="k2">)</span>
<span class="number">  8</span>  <span class="k2">{</span>
<span class="number">  9</span>  std::vector<span class="k3">&lt;</span>std::thread&gt; threads<span class="k2">;</span>
<span class="number"> 10</span>  
<span class="number"> 11</span>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">5</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span>
<span class="number"> 12</span>    <span class="k2">{</span>
<span class="number"> 13</span>    std::thread t1<span class="k2">(</span>task1, <span class="s">"Hello"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 14</span>    threads.push_back<span class="k2">(</span>t1<span class="k2">)</span><span class="k2">;</span> <span class="c">//does not COMPILE </span>
<span class="number"> 15</span>        
<span class="number"> 16</span>    threads.push_back<span class="k2">(</span>std::thread<span class="k2">(</span>task1, <span class="s">"Hello"</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span> <span class="c">//WORKS</span>
<span class="number"> 17</span>    <span class="k2">}</span>
<span class="number"> 18</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Started.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 19</span>  
<span class="number"> 20</span>  <span class="k1">for</span><span class="k2">(</span><span class="k1">auto</span><span class="k3">&amp;</span> t <span class="k2">:</span> threads<span class="k2">)</span>
<span class="number"> 21</span>    <span class="k2">{</span>
<span class="number"> 22</span>    t.join<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 23</span>    <span class="k2">}</span>  
<span class="number"> 24</span>  
<span class="number"> 25</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"Finished.\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>  <span class="k2">}</span>
</div></div><p>

Any ideas?</p><p>Also, I thought you had to &quot;join&quot; the thread to actually run it yet oddly enough they seem to run before join (they print before it prints &quot;Started.&quot;) and I also thought join meant &quot;pause main, run task until completion and then continue here.&quot; which would also imply they should be after started, and in order.</p><p>[edit]</p><p>It should be noted I&#39;m using GCC 4.9.2 on Linux which may have faulty support for std::threads. I learned that the damn hard way after their regex was completely broken while I tried to learn it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 26 May 2015 05:25:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am making a wild guess here but it may be because the first std::thread is created on the stack and has to be copied but the second thread is a temporary object. It may be using move semantics in the push_back function when there is an rvalue instead of an lvalue.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 26 May 2015 05:31:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Please see my note, I posted before I fixed it. The first one^H^H^HLINE doesn&#39;t compile with a huge template error about allocater.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>In file included from <span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>x86_64-linux-gnu<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>bits<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span>allocator.h:33:0,
<span class="number">  2</span>                 from <span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>bits<span class="k3">/</span>allocator.h:46,
<span class="number">  3</span>                 from <span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>string:41,
<span class="number">  4</span>                 from main.cpp:4:
<span class="number">  5</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>ext<span class="k3">/</span>new_allocator.h: In instantiation of <span class="k1">void</span> __gnu_cxx::new_allocator<span class="k3">&lt;</span>_Tp&gt;::construct<span class="k2">(</span>_Up<span class="k3">*</span>, _Args<span class="k3">&amp;</span><span class="k3">&amp;</span> ...<span class="k2">)</span> <span class="k2">[</span>with _Up <span class="k3">=</span> std::thread<span class="k2">;</span> _Args <span class="k3">=</span> <span class="k2">{</span><span class="k1">const</span> std::thread<span class="k3">&amp;</span><span class="k2">}</span><span class="k2">;</span> _Tp <span class="k3">=</span> std::thread<span class="k2">]</span><span class="k2">:</span>
<span class="number">  6</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>bits<span class="k3">/</span>alloc_traits.h:253:4:   required from <span class="k1">static</span> std::_Require<span class="k3">&lt;</span><span class="k1">typename</span> std::allocator_traits<span class="k3">&lt;</span>_Alloc&gt;::__construct_helper<span class="k3">&lt;</span>_Tp, _Args&gt;::type&gt; std::allocator_traits<span class="k3">&lt;</span>_Alloc&gt;::_S_construct<span class="k2">(</span>_Alloc<span class="k3">&amp;</span>, _Tp<span class="k3">*</span>, _Args<span class="k3">&amp;</span><span class="k3">&amp;</span> ...<span class="k2">)</span> <span class="k2">[</span>with _Tp <span class="k3">=</span> std::thread<span class="k2">;</span> _Args <span class="k3">=</span> <span class="k2">{</span><span class="k1">const</span> std::thread<span class="k3">&amp;</span><span class="k2">}</span><span class="k2">;</span> _Alloc <span class="k3">=</span> std::allocator<span class="k3">&lt;</span>std::thread&gt;<span class="k2">;</span> std::_Require<span class="k3">&lt;</span><span class="k1">typename</span> std::allocator_traits<span class="k3">&lt;</span>_Alloc&gt;::__construct_helper<span class="k3">&lt;</span>_Tp, _Args&gt;::type&gt; <span class="k3">=</span> <span class="k1">void</span><span class="k2">]</span>
<span class="number">  7</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>bits<span class="k3">/</span>alloc_traits.h:399:57:   required from <span class="k1">static</span> decltype <span class="k2">(</span>_S_construct<span class="k2">(</span>__a, __p, <span class="k2">(</span>forward<span class="k3">&lt;</span>_Args&gt;<span class="k2">)</span><span class="k2">(</span>std::allocator_traits::construct::__args<span class="k2">)</span>...<span class="k2">)</span><span class="k2">)</span> std::allocator_traits<span class="k3">&lt;</span>_Alloc&gt;::construct<span class="k2">(</span>_Alloc<span class="k3">&amp;</span>, _Tp<span class="k3">*</span>, _Args<span class="k3">&amp;</span><span class="k3">&amp;</span> ...<span class="k2">)</span> <span class="k2">[</span>with _Tp <span class="k3">=</span> std::thread<span class="k2">;</span> _Args <span class="k3">=</span> <span class="k2">{</span><span class="k1">const</span> std::thread<span class="k3">&amp;</span><span class="k2">}</span><span class="k2">;</span> _Alloc <span class="k3">=</span> std::allocator<span class="k3">&lt;</span>std::thread&gt;<span class="k2">;</span> decltype <span class="k2">(</span>_S_construct<span class="k2">(</span>__a, __p, <span class="k2">(</span>forward<span class="k3">&lt;</span>_Args&gt;<span class="k2">)</span><span class="k2">(</span>std::allocator_traits::construct::__args<span class="k2">)</span>...<span class="k2">)</span><span class="k2">)</span> <span class="k3">=</span> <span class="k3">&lt;</span>type error&gt;<span class="k2">]</span>
<span class="number">  8</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>bits<span class="k3">/</span>stl_vector.h:918:34:   required from <span class="k1">void</span> std::vector<span class="k3">&lt;</span>_Tp, _Alloc&gt;::push_back<span class="k2">(</span><span class="k1">const</span> value_type<span class="k3">&amp;</span><span class="k2">)</span> <span class="k2">[</span>with _Tp <span class="k3">=</span> std::thread<span class="k2">;</span> _Alloc <span class="k3">=</span> std::allocator<span class="k3">&lt;</span>std::thread&gt;<span class="k2">;</span> std::vector<span class="k3">&lt;</span>_Tp, _Alloc&gt;::value_type <span class="k3">=</span> std::thread<span class="k2">]</span>
<span class="number">  9</span>main.cpp:33:23:   required from here
<span class="number"> 10</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>ext<span class="k3">/</span>new_allocator.h:120:4: error: use of deleted function std::thread::thread<span class="k2">(</span><span class="k1">const</span> std::thread<span class="k3">&amp;</span><span class="k2">)</span>
<span class="number"> 11</span>  <span class="k2">{</span> <span class="k2">:</span><span class="k2">:</span><span class="k1">new</span><span class="k2">(</span><span class="k2">(</span><span class="k1">void</span> <span class="k3">*</span><span class="k2">)</span>__p<span class="k2">)</span> _Up<span class="k2">(</span>std::forward<span class="k3">&lt;</span>_Args&gt;<span class="k2">(</span>__args<span class="k2">)</span>...<span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span>
<span class="number"> 12</span>    ^
<span class="number"> 13</span>In file included from main.cpp:5:0:
<span class="number"> 14</span><span class="k3">/</span>usr<span class="k3">/</span>include<span class="k3">/</span>c<span class="k3">+</span><span class="k3">+</span><span class="k3">/</span><span class="n">4</span>.<span class="n">9</span><span class="k3">/</span>thread:126:5: note: declared here
<span class="number"> 15</span>     thread<span class="k2">(</span><span class="k1">const</span> thread<span class="k3">&amp;</span><span class="k2">)</span> <span class="k3">=</span> <span class="k1">delete</span><span class="k2">;</span>
<span class="number"> 16</span>     ^
</div></div><p>

And per my thoughts, <a href="http://stackoverflow.com/questions/15148057/what-does-stdthread-join-do">this SO post</a> confirms what join does. But I&#39;ll look into your comments.</p><p>I&#39;m not used to people replying so fast! <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Tue, 26 May 2015 05:32:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You don&#39;t have to join a thread to run, but you do have to start it (in this case perhaps automatically). If the threads have already finished though, join won&#39;t have any effect.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Tue, 26 May 2015 05:36:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, it&#39;s what Edgar said. The copy constructor of <span class="source-code">std::thread</span> is deleted (as it makes no sense to copy a thread, I suppose), so you need to move the variable using <span class="source-code">std::move</span>. I.e. you&#39;d do <span class="source-code">threads.push_back<span class="k2">(</span>std::move<span class="k2">(</span>t1<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Tue, 26 May 2015 06:35:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I suppose it&#39;s helpful that c++11 does that move stuff, but I&#39;d still avoid storing a static std::thread in a container. I generally store non trivial objects in std containers as pointers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 26 May 2015 07:25:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The idiomatic pointer type for this purpose would be <span class="source-code">std::unique_ptr</span> which also needs to be moved <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />. These days I basically never use pointers unless I&#39;m looking for some dynamic polymorphism.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Tue, 26 May 2015 07:34:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not use Allegro&#39;s threads in stead? <br /><a href="https://www.allegro.cc/manual/5/ALLEGRO_THREAD">https://www.allegro.cc/manual/5/ALLEGRO_THREAD</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Tue, 26 May 2015 10:03:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013560#target">SiegeLord</a> said:</div><div class="quote"><p>The idiomatic pointer type for this purpose would be std::unique_ptr which also needs to be moved <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />. These days I basically never use pointers unless I&#39;m looking for some dynamic polymorphism.</p></div></div><p>Eh. I also tend not to mess with std::unique_ptr much either. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 26 May 2015 10:35:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It might also be useful to note that moving a variable will destroy the original and should not be used afterwards, much like in Rust, except Rust enforces it and C++ should but doesn&#39;t.</p><p>Afaik, all <span class="source-code">std::thread</span>s must end before they go out of scope, so you should call <span class="source-code">thread::join</span> before they get destroyed.</p><div class="source-code snippet"><div class="inner"><pre>
<span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">5</span><span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span>
    <span class="k2">{</span>
    std::thread t1<span class="k2">(</span>task1, <span class="s">"Hello"</span><span class="k2">)</span><span class="k2">;</span>
    threads.push_back<span class="k2">(</span>t1<span class="k2">)</span><span class="k2">;</span> <span class="c">// Tries to invoke the deleted copy constructor.</span>
        
    threads.push_back<span class="k2">(</span>std::thread<span class="k2">(</span>task1, <span class="s">"Hello"</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Returns a rvalue.</span>
        <span class="c">// Gets moved implicitly, you might want to use emplace_back to construct the thread directly in the vector and avoid any unnecessary copies.</span>
    std::thread t2<span class="k2">(</span>task1, <span class="s">"Hello"</span><span class="k2">)</span><span class="k2">;</span>
    threads.push_back<span class="k2">(</span>std::move<span class="k2">(</span>t2<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Explicitly move t2</span>
        <span class="c">// t2 should be considered 'consumed' and no longer be used.</span>
    <span class="k2">}</span>
</pre></div></div><p>

Took me a little while to figure these new move semantics out, but once you understand them they&#39;ll seem very elegant, at least that was my experience.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (l j)</author>
		<pubDate>Tue, 26 May 2015 11:42:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013563#target">taron </a> said:</div><div class="quote"><p>It might also be useful to note that moving a variable will destroy the original and should not be used afterwards</p></div></div><p>
IIRC, moving a variable puts the original into a cleared/destructible state. The object is destructed at the end of its scope as normal, so it is still valid after a move. What its actual state is, though, is left undefined (you may or may not be able to do anything with it except let it destruct).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Wed, 27 May 2015 07:10:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I realize my use of destroy might have been a little unclear, especially when I&#39;ve used it with 2 different implied meanings.</p><p>But what I meant with the first instance of destroy is that it effectively can end up destroying the contents of the original and leaves it in an unspecified state, not that it calls the destructor, so you aren&#39;t supposed to use it in that state. Apparently an object you move from is called an xvalue (expiring value) now.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (l j)</author>
		<pubDate>Wed, 27 May 2015 15:29:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I guess I understand std::move now. std::forward is confusing. </p><p>But geez. Everything keeps getting more and more complex. I mean... std::namespace names for actual language semantics now?</p><p>I mean, what&#39;s next, std::addition? I was going to joke and say std::cast, std::reference, std::pointer or std::const, but those actually exist with slight variations in spelling. </p><p>Which means you have to use the std::namespace to use the most basic functionality of C++. I guess that&#39;s not automatically a &quot;bad&quot; thing if you&#39;re &quot;all in&quot; for C++, but it calls into question the philosophy of &quot;only pay for what you use&quot; if you&#39;re including more and more complex parts of the STL just to run a program.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 27 May 2015 18:10:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Nothing stops you from just doing what <span class="source-code">std::move</span> does manually:</p><div class="source-code snippet"><div class="inner"><pre>threads.push_back<span class="k2">(</span><span class="k2">(</span>std::thread<span class="k3">&amp;</span><span class="k3">&amp;</span><span class="k2">)</span>t1<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 27 May 2015 20:21:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, I think I finally get what join means.</p><p>A std::thread will start execution the moment you create it. However, if you want to <i>pause</i> the calling thread until a child thread is finished, you would call join to ensure execution of the parent doesn&#39;t continue until after the child finishes.</p><p>That is, if I have asset_manager.load_data() dispatching a bunch of worker tasks representing the loading of game assets, I certainly don&#39;t want to have asset_manager.load_data() returning or making decisions until that data has finished loading.</p><p>So when I&#39;m playing around with tiny functions, they&#39;re very likely to finish before main() even makes a decision about them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Fri, 29 May 2015 08:39:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Apologies if you already know this, but you can put 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span>
</pre></div></div><p>
just under your #includes to bring everything from <span class="source-code">std::</span> into scope (so you can put <span class="source-code">vector</span> instead of <span class="source-code">std::vector</span>) (<a href="http://www.cplusplus.com/doc/tutorial/namespaces/">link</a>) - but bear in mind your definitions might clash with something from <span class="source-code">std::</span>. You can also do it on a name-by-name basis, e.g
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">using</span> std::vector<span class="k2">;</span>
<span class="k1">using</span> std::thread<span class="k2">;</span>
</pre></div></div><p> </p><p>And apologies again for thread hijack, but when iterating
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span><span class="k2">(</span><span class="k1">auto</span><span class="k3">&amp;</span> t <span class="k2">:</span> threads<span class="k2">)</span> 
<span class="k2">{</span>  
    t.join<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  
<span class="k2">}</span>
</pre></div></div><p>
I&#39;m sure I saw somewhere it should be <span class="source-code"><span class="k1">auto</span><span class="k3">&amp;</span><span class="k3">&amp;</span></span> but I don&#39;t know why or what this means. Any ideas?</p><p>Pete
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Sat, 30 May 2015 15:08:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>auto detects the type from the rhs, and &amp;&amp; tells it to be an rvalue reference. Which can let you take a reference of a rvalue, or temporary, it&#39;s used in implementing move semantics and such.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 30 May 2015 23:34:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You don&#39;t want an <span class="source-code"><span class="k1">auto</span><span class="k3">&amp;</span><span class="k3">&amp;</span></span> in a for loop unless you&#39;re planning on moving out those elements.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sat, 30 May 2015 23:45:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No really, what kind of an insane, cryptic mess is this all? <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><p>I really can&#39;t see how the C++ way with all those problems is easier than just a C array with malloc, realloc, and pointers to Allegro threads...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sun, 31 May 2015 00:50:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The only reason C++ needs this nonsense is because it has overloadable assignment operator. For C, you could say it moves by default!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Sun, 31 May 2015 02:06:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, I see, now that you mention it... That&#39;s why most languages that allow operator overloading still don&#39; t allow the assignment operator to be overloaded. Ruby comes to mind as such a language... </p><p>And yes, C assignment copies the data by default, which is the correct thing to do for a low level language. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sun, 31 May 2015 02:13:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Every time I think I&#39;m getting a grasp on modern C++, it opens up a new steaming pile of complexity. <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Sun, 31 May 2015 02:24:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve still not looked deep into move semantics. So far haven&#39;t needed it <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 31 May 2015 02:28:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013673#target">SiegeLord</a> said:</div><div class="quote"><p> You don&#39;t want an auto&amp;&amp; in a for loop unless you&#39;re planning on moving out those elements. </p></div></div><p>I think maybe it moves <b>only</b> if you give it an rvalue, otherwise it collapses `&amp;&amp;` to `&amp;` and just takes a reference. Have a look at<br /><a href="http://en.cppreference.com/w/cpp/language/range-for">http://en.cppreference.com/w/cpp/language/range-for</a><br />and<br /><a href="http://stackoverflow.com/questions/9162808/what-does-auto-do">http://stackoverflow.com/questions/9162808/what-does-auto-do</a></p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013675#target">beoran</a> said:</div><div class="quote"><p> No really, what kind of an insane, cryptic mess is this all?
</p></div></div><p>Indeed.</p><p>If you look into `&lt;memory&gt;` or any of the standard headers it&#39;s all very complicated stuff - unfortunately for C++ (IMO) you do need to understand a little of how it works to use it efficiently - you can&#39;t just leave it all to the library writers.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013682#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> I&#39;ve still not looked deep into move semantics. So far haven&#39;t needed it
</p></div></div><p>I mostly use it for things like
</p><div class="source-code snippet"><div class="inner"><pre>MyClass inst<span class="k2">;</span>
<span class="c">//... do something with inst ...</span>
myvector.push_back<span class="k2">(</span>std::move<span class="k2">(</span>inst<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
without the move it&#39;ll make a copy of <span class="source-code">inst</span> to put into the vector, then <span class="source-code">inst</span> itself will be destroyed some time later. If <span class="source-code">inst</span> is big/complicated that could be expensive.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Sun, 31 May 2015 13:40:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I personally don&#39;t use <span class="source-code"><span class="k1">auto</span><span class="k3">&amp;</span><span class="k3">&amp;</span></span> outside of template or generic lambdas.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (l j)</author>
		<pubDate>Sun, 31 May 2015 18:08:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013685#target">Peter Hull</a> said:</div><div class="quote"><p>without the move it&#39;ll make a copy of inst to put into the vector, then inst itself will be destroyed some time later. If inst is big/complicated that could be expensive.</p></div></div><p>Ok, makes sense. Mind you I typically use pointers for big complex objects. So it&#39;s kindof a moot point.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615409/1013689#target">taron </a> said:</div><div class="quote"><p>I personally don&#39;t use auto&amp;&amp; outside of template or generic lambdas.</p></div></div><p>I tend to only use auto (forget about &amp;&amp;) when I want to avoid typing out a long string of line noise that they call container iterator types. In that case you don&#39;t even want to make it a reference.</p><p>Now I have used T&amp;&amp; a fair bit in templates. or &quot;Args&amp;&amp;... args&quot;  but thinking about it, that&#39;s probably best to trim down to &quot;Args&amp;... args&quot; or drop the reference all together possibly. I&#39;ll have to test it. If the compiler isn&#39;t collapsing &amp;&amp; down to &amp; there, it&#39;ll case moves in places it won&#39;t be wanted in that code, sooo bad idea <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><p>append: Basically I stole some code from <a href="http://stackoverflow.com/questions/687490/how-do-i-expand-a-tuple-into-variadic-template-functions-arguments">here</a> and modified it a bit.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 31 May 2015 23:00:23 +0000</pubDate>
	</item>
</rss>
