473,499 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to resolve ADL(?) issue using std::copy and std::ostream_iterator

Greetings all. I am really stuck on this one as I can't seem to grok if
I am abusing the C++ language or if I am simply using components of the
C++ Standard Library incorrectly. Here is the code:

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

typedef std::pair<std::string, std::stringSTR;

#ifdef NON_COMPLIANT
// Works but my understanding this is forbidden by
// Standard ISO/IEC 14882.2003 17.4.3.1-1
// since I introduce code into namespace std
namespace std {
{
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
}
#else
// Generates error
// std::copy can't find a suitable candidate for operator<<
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
#endif

int main(int argc, char** argv, char** env) {
std::map<std::string, std::stringtest;
test["foo"] = "bar";
test["baz"] = "qux";

std::ostream_iterator<STRo(std::cout);
std::copy(
test.begin(),
test.end(),
o);

return 0;
}

If compiled with the macro definition NON_COMPLIANT line 12 violates the
Standard section 17.4.3.1-1 commented on in the code but that seems to
be the only way to allow for std::copy to deduce the correct context of
operator<<, is this correct? (I apologize if I am not using the correct
terminology - please educate me accordingly on this as well)

If my understanding above is correct then can I conclude that I simply
do not understand how to use the Standard C++ library correctly in this
case? The code appears to be a logical use of the components at play
but my compiler disagrees with my thinking.

Thank you for reading and any insight you can assist me with in advance.
Chris
Jul 2 '06 #1
6 1946
* Chris Johnson:
Greetings all. I am really stuck on this one as I can't seem to grok if
I am abusing the C++ language or if I am simply using components of the
C++ Standard Library incorrectly. Here is the code:

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

typedef std::pair<std::string, std::stringSTR;

#ifdef NON_COMPLIANT
// Works but my understanding this is forbidden by
// Standard ISO/IEC 14882.2003 17.4.3.1-1
// since I introduce code into namespace std
namespace std {
{
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
}
#else
// Generates error
// std::copy can't find a suitable candidate for operator<<
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
#endif

int main(int argc, char** argv, char** env) {
std::map<std::string, std::stringtest;
test["foo"] = "bar";
test["baz"] = "qux";

std::ostream_iterator<STRo(std::cout);
std::copy(
test.begin(),
test.end(),
o);

return 0;
}

If compiled with the macro definition NON_COMPLIANT line 12 violates the
Standard section 17.4.3.1-1 commented on in the code but that seems to
be the only way to allow for std::copy to deduce the correct context of
operator<<, is this correct? (I apologize if I am not using the correct
terminology - please educate me accordingly on this as well)
There are a number of related active issues at <url:
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-toc.html>, but I'm not sure
whether this is one of them.

Related paper by Howard Hinnant: <url:
http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1387.htm>.

I think this question belongs in [comp.std.c++], and I suggest you post
the question there.

If my understanding above is correct then can I conclude that I simply
do not understand how to use the Standard C++ library correctly in this
case? The code appears to be a logical use of the components at play
but my compiler disagrees with my thinking.
I think you can conclude that /probably/ the standard is a bit defective
in this regard, there being a number of related active issues.

Thank you for reading and any insight you can assist me with in advance.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 2 '06 #2
In article <JbTpg.28904$FR1.15119@dukeread05>,
Chris Johnson <de*********@gmail.comwrote:
Greetings all. I am really stuck on this one as I can't seem to grok if
I am abusing the C++ language or if I am simply using components of the
C++ Standard Library incorrectly. Here is the code:

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

typedef std::pair<std::string, std::stringSTR;

#ifdef NON_COMPLIANT
// Works but my understanding this is forbidden by
// Standard ISO/IEC 14882.2003 17.4.3.1-1
// since I introduce code into namespace std
namespace std {
{
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
}
#else
// Generates error
// std::copy can't find a suitable candidate for operator<<
std::ostream& operator<<(std::ostream& os, STR const& ip) {
os << ip.first << " " << ip.second << std::endl;
return os;
}
#endif

int main(int argc, char** argv, char** env) {
std::map<std::string, std::stringtest;
test["foo"] = "bar";
test["baz"] = "qux";

std::ostream_iterator<STRo(std::cout);
std::copy(
test.begin(),
test.end(),
o);

return 0;
}

If compiled with the macro definition NON_COMPLIANT line 12 violates the
Standard section 17.4.3.1-1 commented on in the code but that seems to
be the only way to allow for std::copy to deduce the correct context of
operator<<, is this correct? (I apologize if I am not using the correct
terminology - please educate me accordingly on this as well)

If my understanding above is correct then can I conclude that I simply
do not understand how to use the Standard C++ library correctly in this
case? The code appears to be a logical use of the components at play
but my compiler disagrees with my thinking.

Thank you for reading and any insight you can assist me with in advance.
Chris
Your understanding is correct in on all counts. The standard is simply
deficient in this area. I had hoped that tuple I/O would save the day
here (pair simply being a special case of tuple length 2). However I/O
was stripped from the tuple library shortly before it was voted into
TR1. tuple is now in the C++0X working draft (still missing I/O). I am
hopeful that it can gain I/O in time for C++0X, but as yet there is no
such concrete proposal.

-Howard
Jul 2 '06 #3
* Howard Hinnant:
I had hoped that tuple I/O would save the day
here (pair simply being a special case of tuple length 2). However I/O
was stripped from the tuple library shortly before it was voted into
TR1. tuple is now in the C++0X working draft (still missing I/O). I am
hopeful that it can gain I/O in time for C++0X, but as yet there is no
such concrete proposal.
I think the problem here is both more general and fundamental: that it
should be valid (not UB) to overload at least some functions in the
'std' namespace, namely, those that serve as customization points.

Perhaps it is already valid, i.e. that somewhere the standard mentions
that it's OK to provide overloads of std::operator<< for user-defined
type (any such wording would be compatible with §17.4.3.1/1, which
refers to exceptions), but I failed to find that. Perhaps ADL "should"
find the user-defined ::operator<<, but at least with my preferred
compiler it didn't, and checking the details in the standard is
laborious work. So I'm assuming the OP's problem is real: ADL doesn't
find ::operator<<, and defining std::operator<< is Strictly Verboten.

Under that assumption: supporting tuple i/o is all well and good, but it
won't solve the OP's problem for some class that can't be expressed as a
tuple; only a more general solution can do that.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 2 '06 #4
In article <4g*************@individual.net>,
"Alf P. Steinbach" <al***@start.nowrote:
Under that assumption: supporting tuple i/o is all well and good, but it
won't solve the OP's problem for some class that can't be expressed as a
tuple; only a more general solution can do that.
The general problem is that the standard has a hole in it. It provides
I/O for all scalar types, and for several standard class types,
including:

basic_string
bitset
complex

But the standard is lacking I/O for several standard types, including:

pair
all of the containers (except string)

This isn't an ADL issue as there are no user-defined types in the OP's
problem. They're all standard types. Indeed, if there were a
user-defined type in the OP's problem, e.g.:

typedef std::pair<MyString, std::stringSTR;

then ADL would save the day.

We could possibly say that it is ok for users to add stuff into
namespace std that does not depend on user-defined types. But I think
eliminating the motivation for doing so by providing commonly needed
functionality (such as I/O) for standard types would be a superior
solution.

-Howard
Jul 2 '06 #5

"Howard Hinnant" <ho************@gmail.comskrev i meddelandet
news:ho**********************************@syrcnyrd rs-03-ge0.nyroc.rr.com...
In article <4g*************@individual.net>,
"Alf P. Steinbach" <al***@start.nowrote:
>Under that assumption: supporting tuple i/o is all well and good,
but it
won't solve the OP's problem for some class that can't be expressed
as a
tuple; only a more general solution can do that.

The general problem is that the standard has a hole in it. It
provides
I/O for all scalar types, and for several standard class types,
including:

basic_string
bitset
complex

But the standard is lacking I/O for several standard types,
including:

pair
all of the containers (except string)

This isn't an ADL issue as there are no user-defined types in the
OP's
problem. They're all standard types. Indeed, if there were a
user-defined type in the OP's problem, e.g.:

typedef std::pair<MyString, std::stringSTR;

then ADL would save the day.

We could possibly say that it is ok for users to add stuff into
namespace std that does not depend on user-defined types.
But how do we know exactly what overload are already provided?
But I think
eliminating the motivation for doing so by providing commonly needed
functionality (such as I/O) for standard types would be a superior
solution.
Right!
Bo Persson
Jul 2 '06 #6
Howard Hinnant wrote:
In article <4g*************@individual.net>,
"Alf P. Steinbach" <al***@start.nowrote:
--8<--[SNIP]--8<--
But the standard is lacking I/O for several standard types, including:

pair
all of the containers (except string)
I think I can say safely say that I now understand this where before
posting I did not.
This isn't an ADL issue as there are no user-defined types in the OP's
problem. They're all standard types. Indeed, if there were a
user-defined type in the OP's problem, e.g.:

typedef std::pair<MyString, std::stringSTR;

then ADL would save the day.
In fact, using a user defined type, it does. I now know what
(correctly) ADL is.

Thanks for the insight to all that have responded - I learned a few
things as a result. I can't thank everyone enough.

Chris
Jul 3 '06 #7

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

Similar topics

9
8868
by: PerritoPerron | last post by:
Hi there fellas.. I am learning this powerful language, but came accross a concatenation problem that I can't resolve. I have 2 arrays A1 = {1,2,3,4}; A2 = {1,3,5,7}; I need to put them...
9
1401
by: ES Kim | last post by:
#include <map> #include <iterator> #include <iostream> #include <algorithm> using namespace std; typedef map<int, int> Map; ostream& operator<<(ostream& os, const Map::value_type& v);
1
2872
by: Siegfried Heintze | last post by:
Should the following work? It does on some compilers. How can I get it to work on g++ 3.2? On g++ 3.2 it keeps telling me that there is no such function operator << for ostream and...
6
338
by: Chris Roth | last post by:
I'm just starting out using the algorithms in c++ std library. I have a vector of vectors (v) and want to get a slice at some index. ex. If v held two vectors, v1 and v2: v1 = 1 2 3 4 5 6 v2 =...
5
3639
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
6
2532
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
16
1625
by: Juha Nieminen | last post by:
The so-called koenig lookup allows doing odd things like this: #include <algorithm> #include <string> int main() { std::string table; sort(table, table+10); }
9
2783
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
42
4461
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.