Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could write a class template <code>auto_iterator_impl</code> and use it through a function template <code>auto_iterator</code> which returns an instance of <code>auto_iterator_impl</code>, which can be implicitly converted into <code>true</code> or <code>false</code>:</p> <p>A working implementation with minimal functionalities and consideration:</p> <pre><code>template&lt;typename C&gt; struct auto_iterator_impl { C &amp; c; typename C::iterator it; auto_iterator_impl(C &amp; c, typename C::iterator &amp; it) : c(c), it(it) {} operator bool() const { return it != c.end(); } typename C::iterator operator-&gt;() { return it; } }; template&lt;typename C&gt; auto_iterator_impl&lt;C&gt; auto_iterator(C &amp; c, typename C::iterator it) { return auto_iterator_impl&lt;C&gt;(c, it); } </code></pre> <p>Test code:</p> <pre><code>void test(std::map&lt;int, int&gt; &amp; m, int key) { if (auto x = auto_iterator(m, m.find(key))) { std::cout &lt;&lt; "found = " &lt;&lt; x-&gt;second &lt;&lt; std::endl; x-&gt;second *= 100; //change it } else std::cout &lt;&lt; "not found" &lt;&lt; std::endl; } int main() { std::map&lt;int,int&gt; m; m[1] = 10; m[2] = 20; test(m, 1); test(m, 3); test(m, 2); std::cout &lt;&lt;"print modified values.." &lt;&lt;std::endl; std::cout &lt;&lt; m[1] &lt;&lt; std::endl; std::cout &lt;&lt; m[2] &lt;&lt; std::endl; } </code></pre> <p>Output:</p> <pre><code>found = 10 not found found = 20 print modified values.. 1000 2000 </code></pre> <p>Online demo : <a href="http://www.ideone.com/MnISh" rel="nofollow">http://www.ideone.com/MnISh</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload