user definitions in the std namespace

Discussion in 'C++' started by John Harrison, Jul 22, 2004.

  1. Is the following code legit?

    #include <iostream>
    #include <utility>
    #include <map>
    #include <algorithm>
    #include <iterator>

    namespace std
    {
    template <class T, class U>
    std::eek:stream& operator<<(std::eek:stream& out, std::pair<T, U> const& p)
    {
    return out << p.first << ' ' << p.second;
    }
    }

    int main()
    {
    std::map<int, int> m;
    std::copy(m.begin(), m.end(),
    std::eek:stream_iterator<std::map<int, int>::value_type>(std::cout));
    }

    No less than three compilers accept this (VC++ 7.1, g++ 3.3.1 and online
    Comeau C++) but it seems that the standard doesn't like it, 17.4.3.1 para
    1 allows template specialisations to be added to std, but not it seems
    function overloads.

    The same three compilers do not accept the same code but with operator<<
    defined in the global namespace. All three give variations on 'no suitable
    operator<< found' error messages.

    So it is possible to define operator<< for a type in the std namespace. If
    so how to do it?

    John
    John Harrison, Jul 22, 2004
    #1
    1. Advertising

  2. On Thu, 22 Jul 2004 20:17:00 +0100, John Harrison
    <> wrote:

    > Is the following code legit?
    >
    > #include <iostream>
    > #include <utility>
    > #include <map>
    > #include <algorithm>
    > #include <iterator>
    >
    > namespace std
    > {
    > template <class T, class U>
    > std::eek:stream& operator<<(std::eek:stream& out, std::pair<T, U> const& p)
    > {
    > return out << p.first << ' ' << p.second;
    > }
    > }
    >
    > int main()
    > {
    > std::map<int, int> m;
    > std::copy(m.begin(), m.end(),
    > std::eek:stream_iterator<std::map<int, int>::value_type>(std::cout));
    > }
    >
    > No less than three compilers accept this (VC++ 7.1, g++ 3.3.1 and online
    > Comeau C++) but it seems that the standard doesn't like it, 17.4.3.1
    > para 1 allows template specialisations to be added to std, but not it
    > seems function overloads.
    >
    > The same three compilers do not accept the same code but with operator<<
    > defined in the global namespace. All three give variations on 'no
    > suitable operator<< found' error messages.
    >
    > So it is possible to define operator<< for a type in the std namespace.
    > If so how to do it?
    >
    > John


    Answering my own question here but in case anyone is interested I found
    this reference from the defect reports.

    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#226

    The note starting "J. C. van Winkel points out ..." directly addresses
    this issue but without deciding whether it is an issue let alone proposing
    a resolution.

    john
    John Harrison, Jul 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,344
    Howard
    Nov 29, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,272
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    7
    Views:
    415
    Jacek Dziedzic
    Oct 3, 2006
  4. bb
    Replies:
    1
    Views:
    294
    Alf P. Steinbach
    Sep 7, 2007
  5. Shriramana Sharma
    Replies:
    2
    Views:
    144
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page