friend operator << overload ambiguity error



 DEVELOP > c-Plus-Plus > friend operator << overload ambiguity error

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Gianni Mariani"
Date: 16 Jan 2005 08:24:24 PM
Object: friend operator << overload ambiguity error
Can anyone enligten me why I get the "ambiguous overload" error from the
code below:
friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,
std::char_traits<char> >& << Thing&' operator
friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]
friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&)
[with
w_char_type = char, w_traits = std::char_traits<char>]
friendop.cpp:27: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]
The code:
#include <iostream>
class Thing;
namespace std {
template<
typename w_char_type,
class w_traits


basic_ostream<w_char_type, w_traits>& operator << (
basic_ostream<w_char_type, w_traits> & o_ostream,
const ::Thing & value
);
}// namespace std
class Thing
{
template<
typename w_char_type,
class w_traits


friend std::basic_ostream<w_char_type, w_traits>& operator << (
std::basic_ostream<w_char_type, w_traits> & o_ostream,
const ::Thing & value
);
};
int main()
{
Thing b;
std::cout << "b = " << b;
}
.

User: "Dave Moore"

Title: Re: friend operator << overload ambiguity error 17 Jan 2005 03:24:45 AM
"Gianni Mariani" <gi2nospam@mariani.ws> wrote in message
news:-v6dnTvdzbpHvHbcRVn-sg@speakeasy.net...


Can anyone enligten me why I get the "ambiguous overload" error from the
code below:

friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,
std::char_traits<char> >& << Thing&' operator
friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&) [with
w_char_type = char, w_traits = std::char_traits<char>]

[snip]

The code:

#include <iostream>

class Thing;
namespace std {

This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning standard. As
soon as you start adding names to it, it will behave differently for you
than for the rest of the world, and so it is then by definition not standard
anymore.
Also, in this case it is the source of your problem. You "polluted" std
with your declaration of operator<< for Thing, and then in the class
declaration of Thing, you forgot to qualify the friend declaration of
operator << to be the one you put in std. Thus you implicitly declared a
second operator<<, which caused the ambiguity flagged by your compiler.

class Thing
{
template<
typename w_char_type,
class w_traits


The following declares a new operator<< outside of std namespace

friend std::basic_ostream<w_char_type, w_traits>& operator << ( >

std::basic_ostream<w_char_type, w_traits> & o_ostream,

const ::Thing & value
);

};

You can fix this error simply by taking your operator<< declaration out of
std, where you never should have put it in the first place.
HTH,
Dave Moore
.
User: "Gianni Mariani"

Title: Re: friend operator << overload ambiguity error 17 Jan 2005 10:52:36 AM
Dave Moore wrote:
....

class Thing;
namespace std {



This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning standard. As
soon as you start adding names to it, it will behave differently for you
than for the rest of the world, and so it is then by definition not standard
anymore.

I had problems ( a while back, I don't remember exactly now) not having
the << operator in the std namespace.
Anyhow, "pollution" is an issue when there is a possibility of
"polluting" causing inadvertent use. In this case operator<< is
overloaded with a parameter type that is specific to the application
which makes it impossible to be confused with another parameter type.
i.e. Overloading for a specific type is non-polluting since it can't
really be mis-applied.


Also, in this case it is the source of your problem. You "polluted" std
with your declaration of operator<< for Thing, and then in the class
declaration of Thing, you forgot to qualify the friend declaration of
operator << to be the one you put in std.

Ah... yep. Missed that one. Thanks.


You can fix this error simply by taking your operator<< declaration out of
std, where you never should have put it in the first place.

I need to go and figure out what the issue was that led me to put it in
there.
....
I now can't reproduce the error I was seeing (a while back). Out of
std:: it goes...
.

User: "Jerry Coffin"

Title: Re: friend operator << overload ambiguity error 17 Jan 2005 11:05:22 AM
[ ... ]

class Thing;
namespace std {


This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning

standard. As

soon as you start adding names to it, it will behave differently for

you

than for the rest of the world, and so it is then by definition not

standard

anymore.

This isn't entirely true -- the standard gives explicit permission for
users to add names to namespace std under the right circumstances. I
don't have my copy of the standard here at work to give all the
details, but if memory serves, overloading on a UDF fulfills one of the
major requirements.
--
Later,
Jerry.
.
User: "Dave Moore"

Title: Re: friend operator << overload ambiguity error 18 Jan 2005 10:16:36 AM
"Jerry Coffin" <jcoffin@taeus.com> wrote in message
news:1105981522.835449.237100@c13g2000cwb.googlegroups.com...

[ ... ]

class Thing;
namespace std {


This is a big no-no ... we users are not allowed to "pollute" the std
namespace. That is the whole point of calling it std, meaning

standard. As

soon as you start adding names to it, it will behave differently for

you

than for the rest of the world, and so it is then by definition not

standard

anymore.


This isn't entirely true -- the standard gives explicit permission for
users to add names to namespace std under the right circumstances. I
don't have my copy of the standard here at work to give all the
details, but if memory serves, overloading on a UDF fulfills one of the
major requirements.

You are absolutely right .. I have heard the wisdom espoused in my earlier
"preachy" post so many times that I just regurgitated it without checking.
This is what the Standard has to say about what can and cannot be added to
namespace std (17.4.3.1/1):
"It is undefined for a C++ program to add declarations or definitions to
namespace std or namespaces within namespace std unless otherwise specified.
A program may add template specializations for any standard library template
to namespace std. 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."
The OP's declaration is for a new template function, not a specialization,
so it does not meet the criteria described above. I also checked out the
section on output streams (27.6.2), where basic_ostream is defined, and I
didn't see any further relaxation of the "user cannot inject names into std
namespace" restriction. Thus it seems he is "not allowed" to put his
declaration in std after all.
So, although my general statement was inaccurate, I think the overall
sentiment was on the mark. A relevant question to the OP is also why he
thought he needed to put his operator<< in std anyway .. I have lots of
similar definitions, and they are all in my own namespaces (and very
occasionally in the global namespace).
Hope this clears things up ...
Dave Moore
<gives self "two for preaching" .... *smack* ... *smack* >
.
User: "Jerry Coffin"

Title: Re: friend operator << overload ambiguity error 18 Jan 2005 01:11:02 PM
[ ... ]

The OP's declaration is for a new template function, not a

specialization,

so it does not meet the criteria described above. I also checked out

the

section on output streams (27.6.2), where basic_ostream is defined,

and I

didn't see any further relaxation of the "user cannot inject names

into std

namespace" restriction. Thus it seems he is "not allowed" to put his
declaration in std after all.

Right -- I didn't intend to say that the basic idea was wrong, or that
the OP's code was right, merely that when you pointed out his error,
you went on to over-generalize to the point of saying something that
was wrong, and would be misleading if taken on its own.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER