Boost C++ Libraries: Ticket #8668: BOOST_FOREACH breaks on clang 3.2 but not on gcc 4.7.2 https://svn.boost.org/trac10/ticket/8668 <p> Consider this snippet: </p> <pre class="wiki">#include &lt;vector&gt; #include &lt;boost/foreach.hpp&gt; class A : public std::vector&lt;int&gt; { public: template &lt;class T&gt; operator const T &amp;() const; }; void foo(){ A colA; int b = 1; BOOST_FOREACH(b, colA) { ; } } </pre><p> clang 3.2 throws this error: </p> <pre class="wiki">error: conditional expression is ambiguous; 'rvalue_probe&lt;A&gt;' can be converted to 'A' and vice versa BOOST_FOREACH(b, colA) expanded from macro 'BOOST_FOREACH' f (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) expanded from macro 'BOOST_FOREACH_CONTAIN' BOOST_FOREACH_EVALUATE(COL) expanded from macro 'BOOST_FOREACH_EVALUATE' (true ? boost::foreach_detail_::make_probe((COL), BOOST_FOREACH_ID(_foreach_is_rvalue)) : (COL)) </pre><p> gcc in version 4.7.2 compiles this snippet with no error. Is this a bug of gcc, clang or boost? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8668 Trac 1.4.3 viboes Sat, 08 Jun 2013 16:52:45 GMT component changed; owner set https://svn.boost.org/trac10/ticket/8668#comment:1 https://svn.boost.org/trac10/ticket/8668#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Eric Niebler</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">foreach</span> </li> </ul> Ticket Eric Niebler Tue, 23 Jul 2013 05:40:18 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8668#comment:2 https://svn.boost.org/trac10/ticket/8668#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> It's not really a bug in gcc, clang, or boost. It's because in C++03, there's no perfect way to tell rvalues from lvalues. <code>BOOST_FOREACH</code> jumps through hoops of fire to figure it out, and it gets it right %100 of the time, +/- some epsilon. Congrats, you've found the epsilon. ;-) </p> <p> If your container has an unconstrained conversion operator template, like <code>A</code> does above, it's not going to work. I think clang is right to reject it. The workaround is to compile with <code>-std=gnu++11</code> or use C++11's range-based <code>for</code> loop. I'm afraid there's no way I know to make this work in C++03. </p> <p> I encourage you to migrate to C++11. <code>BOOST_FOREACH</code> is legacy. </p> Ticket