473,803 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator >> needs to be in namespace std??

template < typename T >
std::istream & operator >(std::istrea m & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
....
std::istream_it erator< std::pair<size_ type, size_type
in_beg(std::cin ), in_end;
....
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you're not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?

Dec 18 '06 #1
11 2064
Noah Roberts wrote:
template < typename T >
std::istream & operator >(std::istrea m & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_it erator< std::pair<size_ type, size_type
in_beg(std::cin ), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you're not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?
Looks like an ADL thing. Care to post a complete example so I can try to
compile it here? I want to be sure I'm doing things exactly as you are.
I'm not sure what the ramifications of std::operator>> (...) might be.

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 18 '06 #2
Noah Roberts wrote:
template < typename T >
std::istream & operator >(std::istrea m & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_it erator< std::pair<size_ type, size_type
in_beg(std::cin ), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you're not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?
I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this). There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can't remember how definitive the discussion was.
Dec 18 '06 #3
Gianni Mariani wrote:
Noah Roberts wrote:
>template < typename T >
std::istream & operator >(std::istrea m & in, std::pair<T,T& p)
{
in >p.first >p.second;
return in;
}
...
std::istream_i terator< std::pair<size_ type, size_type
in_beg(std::ci n), in_end;
...
fails to compile. Wrapping the operator >for the pair in namespace
std {} works. Since you're not "allowed" to insert stuff into
namespace std why is that seemingly required and how could this be done
without that?

I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this).
Curiously, this works:

#include <iostream>

template < typename T >
std::ostream& operator<<(std: :ostream& out, std::pair<T,T>& p) {
return out << p.first << p.second;
}
int main() {
std::pair<int,i ntp(1,4);
std::cout<<p<<s td::endl;
}

In truth, even if adding the symbol to std works and is "acceptable ", I
would like to know why it is necessary.
There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can't remember how definitive the discussion
was.
I defer to the Standard.
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 18 '06 #4

Gianni Mariani wrote:
I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this). There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can't remember how definitive the discussion was.
Took a sec to find it but it's there:

17.4.3.1 is where it states a program can't add declarations or
definitions to namespace std. However, it states you *can* add
template specializations . "Such a specialization (complete or partial)
of a standard library template results in undefined behavior unless the
declaration depends on a user-defined name of external linkage and
unless the specialization meets the standard library requirements for
the original template."

I'm a little confused here though. Why is it *necissary* that operator
>be defined in namespace std? Does that function qualify as a template specialization?
Dec 18 '06 #5
Noah Roberts wrote:
Gianni Mariani wrote:
>I thought you were allowed to add things to the std namespace (namely
overloads of pre-existing operators - just like this). There was a
discussion about this a couple of years ago, and I do remember two sides
of the argument, however I can't remember how definitive the discussion
was.

Took a sec to find it but it's there:

17.4.3.1 is where it states a program can't add declarations or
definitions to namespace std. However, it states you *can* add
template specializations . "Such a specialization (complete or partial)
of a standard library template results in undefined behavior unless the
declaration depends on a user-defined name of external linkage and
unless the specialization meets the standard library requirements for
the original template."

I'm a little confused here though. Why is it *necissary* that operator
>>be defined in namespace std? Does that function qualify as a template
specializatio n?
It does not count as a template specialization. It is a definition of a
brand new template. And I am afraid, reading the text of the standard you
posted, that it is not "legal".

However there is another "rule" you break: do not use std::pair for your own
types. Use them as members, or private inheritance, but not directly as a
user defined type.

For the practical side: if you are only using this printing function as a
debugging tool, I would say you are quite safe and it will work as expected.

....I am just thinking that does a template instance of std::pair count as
user defined type... But I don't think it does. It would not make sense.
But I am not a language lawyer so I might be very wrong.
--
WW aka Attila
:::
Business - the art of extracting money from another man's pocket without
resorting to violence. -- Max Amsterdam
Dec 19 '06 #6
White Wolf schrieb:
However there is another "rule" you break: do not use std::pair for your own
types. Use them as members, or private inheritance, but not directly as a
user defined type.
Why not ?
...
Stefan
--
Stefan Naewe
stefan_DOT_naew e_AT_atlas_DOT_ de
Dec 19 '06 #7

Stefan Naewe wrote:
White Wolf schrieb:
However there is another "rule" you break: do not use std::pair for your own
types. Use them as members, or private inheritance, but not directly as a
user defined type.

Why not ?
bump...

yeah, why not?

Dec 23 '06 #8
Noah Roberts wrote:
>
Stefan Naewe wrote:
White Wolf schrieb:
However there is another "rule" you break: do not use std::pair
for your own types. Use them as members, or private inheritance,
but not directly as a user defined type.
Why not ?

bump...
"Bump"? What does that mean?

Brian
Dec 23 '06 #9
Noah Roberts wrote:
Stefan Naewe wrote:
>White Wolf schrieb:
>>However there is another "rule" you break: do not use std::pair
for your own types. Use them as members, or private inheritance,
but not directly as a user defined type.

Why not ?

bump...

yeah, why not?
Because std::pair isn't a user defined type? :-)

One problem in particular is that it doesn't have operator>and operator<<
defined, and you are formally not allowed to define those inside namespace
std. :-)))

Bo Persson
Dec 24 '06 #10

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

Similar topics

5
2124
by: Roger Leigh | last post by:
I've written a simple container template class to contain a single value. This emits a signal when the value is changed (it's used as a notifier of changes), and listeners can connect to its changed signal. i.e. field<int> i(2); i = 4; // field<int>::m_value = 4; changed signal is emitted. Currently, the contained value may be accessed via get_value() and set_value() methods, and for ease of use, operator= and some type conversions...
7
9192
by: John M | last post by:
Can someone tell how to fix my visual c++ so it can discard this error message. I get this error message error C2593: 'operator >>' is ambiguous But I do not when I am on my mandrake linux platform. Why is this, a bug in visual c++ ? John J
3
2055
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
6
1925
by: n2xssvv g02gfr12930 | last post by:
Does anyone know of an example of overloading operator ->* Cheers JB
0
1716
by: heplesser | last post by:
Summary: Does the C++ standard require std::basic_istream<>::operator>>(double&) to leave the input stream untouched in case of a read failure? Details: I noticed an unexpected behavior of operator>>() for numbers (double, int) when reading from cin. I would like to ask for expert clarification on whether I am misunderstanding the rules of the game, or whether my library implementation has a bug. I tested this on g++
8
1965
by: devphylosoff | last post by:
I can write friend bool operator<(const Integer& left, const Integer& right); but i cannot use virtual bool operator<(const Integer& left, const Integer& right); or bool operator<(const Integer& left, const Integer& right); no.cpp:10: error: bool Integer::operator<(const Integer&, const
6
2941
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the raw pointer at "the end of the arrow". For types that support the operator, I have a reasonable solution, but I'd like to have a null pointer returned if the operator isn't supported by a particular object.
0
1258
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector (x1, y1, x2, y2, ...). The iterator should return only pairs of doubles to avoid possible confusion of the user of the class Polygon. (The storage format is needed for conversion to other libraries.) The code for Polygon below compiles well, but...
9
2927
by: Nico | last post by:
I want to create a const_iterator that gives me access to dynamically generated objects. For example see the class Polygon that contains a list of points stored as following doubles in a vector (x1, y1, x2, y2, ...). The iterator should return only pairs of doubles to avoid possible confusion of the user of the class Polygon. (The storage format is needed for conversion to other libraries.) The code for Polygon below compiles well, but...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10300
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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 we have to send another system
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.