27

For example uninitialized_copy is defined in the standard as:

Effects:

for (; first != last; ++result, ++first)
  ::new (static_cast<void*>(&*result))
    typename iterator_traits<ForwardIterator>::value_type(*first);

If understood literally, this is a requirement to call operator ,(ForwardIterator, InputIterator). And in fact this code prints Hello world! ten times:

#include <memory>
#include <iterator>
#include <iostream>
 
using namespace std;

namespace N {     
    struct X : iterator<forward_iterator_tag, int> {
        pointer _p;
        X(pointer p) : _p(p) {}
        X& operator++() { ++_p; return *this; }
        X operator++(int) { X r(*this); ++_p; return r; }
        reference operator*() const { return *_p; }
        pointer operator->() 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 << "Hello world!\n"; }
}

int a[10], b[10];
 
int main()
{
    using N::X;
    uninitialized_copy(X(a), X(a+10), X(b));
}

However, for most other algorithms the standard gives the description in prose. E.g. for copy there's no requirement for operator , to be called. But if I change

    uninitialized_copy(X(a), X(a+10), X(b));

in the above code to

    copy(X(a), X(a+10), X(b));

then Hello world! is still printed ten times. The said results are observable in both, VS2005 and GCC 4.3.4. However, if I write

    mismatch(X(a), X(a+10), X(b));

instead, then VS2005 prints Hello world! ten times but GCC does not.

Unfortunately I couldn't find where the standard prohibits operator, overloading for iterator types. On the contrary, it prohibits the implementations to do calls as above [global.functions]:

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).

So who of the four parties is wrong: MSVC, GCC, ISO or me? (Choose one)

4
  • 3
    I think Visual C++, gcc, and ISO are all wrong: Visual C++ and gcc should not be using the comma operator, and the spec (ISO) should not use the comma in its example code. I could be wrong; that's just my first thought on the matter. (Please consider opening a Visual C++ bug on Microsoft Connect; it's at least worth bringing to their attention.) Jan 3, 2012 at 23:04
  • 2
    Given that even something like Size is a templated type, I'd turn the argument on its head and say that the standard wants precisely what it says, and if you see reason to provide a custom comma operator, then you are invited to do so, and you will get the specified behaviour.
    – Kerrek SB
    Jan 3, 2012 at 23:15
  • @JamesMcNellis KerrekSB: Thank you. I hope someone can find a place where it's said to be undefined behavior, otherwise we have three bug reports to open. Jan 3, 2012 at 23:18
  • @ybungalobill: FYI you can't notify two people in one comment. Jan 4, 2012 at 1:19

1 Answer 1

4

Nice catch. I think in my humble opinion that the ISO committee's intention was that §3.4.2 should be followed. The suggested semantics of uninitialized_copy is wrongly interpreted as if requiring the comma to be called. And implementations should not be using it (I'd report a bug to gcc btw).

2
  • 3
    This is my interpretation as well. Comma overloading is crazy and I think it just slipped past our commitee members' attention. Jan 4, 2012 at 8:11
  • Yeah, since then I submitted a PR and AFAIK they fixed the standard and the popular implementations followed removing the unintended comma operator calls. Aug 23, 2016 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.