title: How to iterate (outside a class) a private member which is a vector<unique_ptr<T>>
body: <pre><code>class Organization {
   private:
    vector&lt;unique_ptr&lt;Employee&gt;&gt; employees_;
    vector&lt;unique_ptr&lt;Item&gt;&gt; items_;
  }org;
  </code></pre>
  
  <p>I need to have a facility outside the class to iterate over the employees and items and call their members, like the following...</p>
  
  <pre><code>for(auto const&amp; e : getEmployees()) { e.get()-&gt;getName(); } 
  </code></pre>
  
  <p>But I cannot make <code>getEmployees()</code> function to return <code>employees_</code>, as vector of unique_ptrs is not copyable</p>
  
  <p>So currently I'm having a for_each_employee function</p>
  
  <pre><code>template&lt;class callable&gt;
  void Organization::for_each_employee(callable f) {
       for(auto const&amp; e : employees_) {
          f(e.get());
     }
  }
  
  // And I use it like,
  org.for_each_employee([](Employee* e){ e-&gt;getName(); });
  </code></pre>
  
  <p>But I do not like this idea as I'll have to write for_each_employee and for_each_item. I have similar other classes of the similar structure. So I will end up writing lot of for_each_XX type functions. This is not what I want. </p>
  
  <p>Can I have generic for_each function that are friends of these classes (like Organization that contain vector of unique_ptrs)?</p>
  
  <p>How to iterate (outside a class) a private member which is a <code>vector&lt;unique_ptr&lt;T&gt;&gt;</code></p>