title: Cannot iterate on a non-copyable container returned by a function
body: <p>I'm not sure of the title, because I'm not sure the issue comes from the "copyablility" of my container.
  I tryied quite everything but I can't get rid of this error.</p>
  
  <p>Here is a simplified version of my code (please do not challenge the class design, I really would like to keep the end-used syntax in the BOOST_FOREACH):</p>
  
  <pre><code>template &lt;typename T&gt;
  class MyContainer
  {
  public:
      typedef typename std::vector&lt;T&gt;::iterator iterator;
      typedef typename std::vector&lt;T&gt;::const_iterator const_iterator;
  
      MyContainer(std::vector&lt;T&gt;&amp; vec, boost::mutex&amp; mutex) :
          m_vector(vec),
          m_lock(mutex)
      {
      }
  
      iterator begin() { return m_vector.begin(); }
      const_iterator begin() const { return m_vector.begin(); }
      iterator end() { return m_vector.end(); }
      const_iterator end() const { return m_vector.end(); }
  
  
  private:
      std::vector&lt;T&gt;&amp; m_vector;
      boost::lock_guard&lt;boost::mutex&gt; m_lock;
  };
  
  template &lt;typename T&gt;
  struct GetContainer
  {
      GetContainer(std::vector&lt;T&gt;&amp; vec, boost::mutex&amp; mutex) :
          m_vector(vec),
          m_mutex(mutex)
      {
      }
  
      MyContainer&lt;T&gt; Get()
      {
          return MyContainer&lt;T&gt;(m_vector, m_mutex);
      }
  
      std::vector&lt;T&gt;&amp; m_vector;
      boost::mutex&amp; m_mutex;
  };
  
  
  
  int main()
  {
      std::vector&lt;int&gt; v;
      v.push_back(1);
      v.push_back(2);
      boost::mutex m;
  
      GetContainer&lt;int&gt; getter(v, m);
  
      BOOST_FOREACH(int i, getter.Get())
      {
          std::cout &lt;&lt; i &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The compiler complains about not having a copy constructor for MyContainer::MyContainer(const MyContainer&amp;).
  I also have :
  error: no matching function for call to ‘MyContainer::MyContainer(boost::foreach_detail_::rvalue_probe >::value_type)’</p>
  
  <p>I follow the extensibility tips:
  <a href="http://www.boost.org/doc/libs/1_58_0/doc/html/foreach/extensibility.html#foreach.extensibility.making__literal_boost_foreach__literal__work_with_non_copyable_sequence_types" rel="nofollow">http://www.boost.org/doc/libs/1_58_0/doc/html/foreach/extensibility.html#foreach.extensibility.making__literal_boost_foreach__literal__work_with_non_copyable_sequence_types</a></p>
  
  <p>But, making </p>
  
  <pre><code>MyContainer&lt;T&gt; : private boost::noncopyable
  </code></pre>
  
  <p>doesn't solve the issue.
  Nor defining the function</p>
  
  <pre><code>boost_foreach_is_noncopyable
  </code></pre>
  
  <p>or specializing the template struct</p>
  
  <pre><code>is_noncopyable
  </code></pre>
  
  <p>for MyContainer (in fact, how would I specialize this template for a template type ?)</p>
  
  <p><strong>Last "tip"</strong>:
  If I remove the mutex and the lock from everywhere (I just pass the vector to GetContainer and to MyContainer), it works.
  But it doesn't work if I make</p>
  
  <pre><code>MyContainer&lt;T&gt; : private boost::noncopyable
  </code></pre>
  
  <p>(I expected it should, so I'm not sure my problem is with BOOST_FOREACH, but maybe because I return a copy of MyContainer with my getter ?)</p>
  
  <p>I thank you if you read me until here, and thanks in advance for help.</p>