问题
The following non-template code works well:
struct A { };
struct B
{
B() {}
B(const A&) {}
friend B operator+(const B&) { return B(); }
};
B operator+(const B&);
int main()
{
A a;
B b;
+b;
+a;
}
But if I make classes in this code templated:
template <class T>
struct A { };
template <class T>
struct B
{
B() {}
B(const A<T>&) {}
friend B operator+(const B&) { return B(); }
};
template <class T>
B<T> operator+(const B<T>&); // not really what I want
int main()
{
A<int> a;
B<int> b;
+b;
+a;
}
some kind of troubles appear:
error: no match for 'operator+' (operand type is 'A<int>')
Is it possible to declare non-template friend function for template class outside the class (as I did for non-template classes above)?
I can solve the problem by adding template operator for A<T>
argument and call friend function inside, but it is not interesting.
UPD:
Another workaround (inspired by R Sahu's answer) is add friend
declaration for class A
:
template <class T>
struct A {
friend B<T> operator+(const B<T>&);
};
but this gives a warning, and I don't know how to fix it correctly.
回答1:
Is it possible to declare non-template friend function for template class outside the class (as I did for non-template classes above)?
Yes, it is possible. However, what you probably need is a function template too, and make sure that operator+<int>
is a friend
of A<int>
, operator+<double>
is a friend
of A<double>
, etc.
See https://stackoverflow.com/a/35854179/434551 to understand how that can be done.
回答2:
Is it possible to declare non-template friend function for template class outside the class?
Answer: yes, but... you need to declare a function for each one combinations you use:
This funcion solves the problem for your puntual declaration:
A<int> operator+(const A<int>&) { return A<int>(); }
But, if you use A< double>, you need to explicity declare other function:
A<double> operator+(const A<double>&) { return A<double>(); }
Is not requiered friend because in a struct all member are public
来源:https://stackoverflow.com/questions/36021863/declare-non-template-friend-function-for-template-class-outside-the-class