473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

user definitions in the std namespace

Is the following code legit?

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

namespace std
{
template <class T, class U>
std::ostream& operator<<(std::ostream& 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::ostream_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
Jul 22 '05 #1
1 1330
On Thu, 22 Jul 2004 20:17:00 +0100, John Harrison
<jo*************@hotmail.com> wrote:
Is the following code legit?

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

namespace std
{
template <class T, class U>
std::ostream& operator<<(std::ostream& 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::ostream_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/wg...fects.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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Magnusson | last post by:
I have a problem with multiple definitions that I can't quite straighten out. I have a templated class defined inside a namespace, and I want to create a function in that namespace that works on...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
5
by: Kobu | last post by:
Does anyone know how old style function definitions differ in "behaviour" to new style function definitions? I heard the old style function definitions caused integeral promotion and floating...
2
by: Gerard Kramer | last post by:
Hello, There is a slight problem with operator overloading in a program I attempt to start practising C++. It is a basic (not very original) game of life simulator. It uses two classes:...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
2
by: James Brown | last post by:
All, Could anyone explain why the following is an error: void foo( struct FOO { int x; } f1 ) { } int main()
2
by: Jeff Johnson | last post by:
Disclaimer: I am extremely new to Web services and may very well be doing the wrong thing! Background: I have a Web service and a Windows Forms client app (VS 2005). The Web service exposes a...
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
3
by: =?Utf-8?B?QW5kcmVhcw==?= | last post by:
If i click on the function "Collapse to definitions" ALL the collapsable elements in the source become collapsed. This is a bug, then it is not correct. If you do it in c# 2005, then namespace...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.