Note that there are some explanatory texts on larger screens.

plurals
  1. POShould the implementation guard itself against comma overloading?
    text
    copied!<p>For example <code>uninitialized_copy</code> is defined in the standard as:</p> <blockquote> <p><em>Effects:</em></p> <pre><code>for (; first != last; ++result, ++first) ::new (static_cast&lt;void*&gt;(&amp;*result)) typename iterator_traits&lt;ForwardIterator&gt;::value_type(*first); </code></pre> </blockquote> <p>If understood literally, this is a requirement to call <code>operator ,(ForwardIterator, InputIterator)</code>. And in fact this code prints <code>Hello world!</code> ten times:</p> <pre><code>#include &lt;memory&gt; #include &lt;iterator&gt; #include &lt;iostream&gt; using namespace std; namespace N { struct X : iterator&lt;forward_iterator_tag, int&gt; { pointer _p; X(pointer p) : _p(p) {} X&amp; operator++() { ++_p; return *this; } X operator++(int) { X r(*this); ++_p; return r; } reference operator*() const { return *_p; } pointer operator-&gt;() const { return _p; } }; bool operator==(X a, X b) { return a._p == b._p; } bool operator!=(X a, X b) { return !(a == b); } void operator,(X a, X b) { cout &lt;&lt; "Hello world!\n"; } } int a[10], b[10]; int main() { using N::X; uninitialized_copy(X(a), X(a+10), X(b)); } </code></pre> <p>However, for most other algorithms the standard gives the description in prose. E.g. for <code>copy</code> there's no requirement for operator <code>,</code> to be called. But if I change</p> <pre><code> uninitialized_copy(X(a), X(a+10), X(b)); </code></pre> <p>in the above code to</p> <pre><code> copy(X(a), X(a+10), X(b)); </code></pre> <p>then <code>Hello world!</code> is <em>still printed ten times</em>. The said results are observable in both, VS2005 and GCC 4.3.4. However, if I write</p> <pre><code> mismatch(X(a), X(a+10), X(b)); </code></pre> <p>instead, then VS2005 prints <code>Hello world!</code> ten times but GCC does not.</p> <p>Unfortunately I couldn't find where the standard prohibits <code>operator,</code> overloading for iterator types. On the contrary, it prohibits the implementations to do calls as above [global.functions]:</p> <blockquote> <p>Unless otherwise specified, global and non-member functions in the standard library shall not use functions from another namespace which are found through argument-dependent name lookup (3.4.2).</p> </blockquote> <p>So who of the four parties is wrong: MSVC, GCC, ISO or me? (Choose one)</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