friend operator << overload ambiguity error

G

Gianni Mariani

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::eek:perator<<(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_traitsbasic_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_traitsfriend 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;
}
 
D

Dave Moore

Gianni Mariani said:
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 said:
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
 
G

Gianni Mariani

Dave Moore wrote:
....
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...
 
J

Jerry Coffin

[ ... ]
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.
 
D

Dave Moore

Jerry Coffin said:
[ ... ]
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* >
 
J

Jerry Coffin

[ ... ]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top