namespaces, classes and template friend functions

M

mojmir

hello,

i have problem with following code (see below). compiler (vs2005sp1)
complains
that the call to fn is ambiguous:
error C2668: 'BB::fn' : ambiguous call to overloaded function
could be 'void BB::fn<dummy>(T &,BB::foo &)'
or 'void AA::fn<dummy>(T &,BB::foo &)'

okay, i have said, and tried to be more specific about the namespace
the fn<> lives in, i.e. from
template<class T> friend void fn(T &, foo &);
to
template<class T> friend void AA::fn(T &, foo &);

wow:
void AA::fn(T &,BB::foo &)' : symbol cannot be defined within
namespace 'BB'
defined? who? where? i merely declared the function to be friend, no?

best regards,
mojmir


=8<===================================================

namespace BB {
class foo;
}

namespace AA {
template<class T> void fn(T &, BB::foo &);
}

namespace BB {
class foo {
template<class T> friend void fn(T &, foo &);
};
}

namespace AA {
template<class T> void fn (T &, BB::foo &) { }
}

class dummy { };

void wmain() {
using namespace BB;
using namespace AA;
foo f;
dummy d;
fn(d, f);
}
 
Z

Zeppe

mojmir said:
hello,

i have problem with following code (see below). compiler (vs2005sp1)
complains
that the call to fn is ambiguous:
error C2668: 'BB::fn' : ambiguous call to overloaded function
could be 'void BB::fn<dummy>(T &,BB::foo &)'
or 'void AA::fn<dummy>(T &,BB::foo &)'

okay, i have said, and tried to be more specific about the namespace
the fn<> lives in, i.e. from
template<class T> friend void fn(T &, foo &);
to
template<class T> friend void AA::fn(T &, foo &);

ehm, I guess the error refers to the call
fn(d, f);
that should be
AA::fn(d, f);

Best wishes,

Zeppe
 
Z

Zeppe

Zeppe said:
ehm, I guess the error refers to the call

My bad, I didn't pay attention before answering. I guess the problem is
that the friend template function declaration is a template declaration,
and you cannot declare a function as belonging to a different namespace.
Unfortunately, when you specify a friend template function I don't think
there is any way to avoid making a template declaration, so I don't know
how to solve the problem (if it can be).

Best wishes,

Zeppe
 
M

mojmir

Zeppe wrote:

there is any way to avoid making a template declaration, so I don't know
how to solve the problem (if it can be).

well fortunately in my case i can simply move the AA::fn into
namespace BB
thus removing ambiguity, but i wonder whether 'dragging' the function
fn as
injected friend into BB's namespace is correct.

mojmir
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top