C++ restrictive template friend function in template class - c++

Consider the code below:
// no forward declarations
//
// template<typename T>
// class A;
//
// template<typename T>
// void swap(A<T>& lhs, A<T>& rhs);
template<typename T>
class A {
int m_i;
public:
// template<typename T1>
// friend void swap<T1>(A<T1>& lhs, A<T1>& rhs); // all instantiations of swap()
// are friends: not restrictive
// friend void swap<>(A<T>& lhs, A<T>& rhs); // restrictive: only T instantiation
// is a friend, but it requires
// forward declaration
friend void swap<>(A& lhs, A& rhs); // note absence of brackets in arguments:
// restrictive as above, but no forward
// declaration required. Why not??
A(int i = 0) : m_i{i} { }
};
template<typename T>
void swap(A<T>& lhs, A<T>& rhs) {
A<double> cA;
// cA.m_i = 0; // compile-time error: swap<int> instantiation
// is NOT a friend of A<double>
//...
}
int main() {
A<int> cA1, cA2; // A<int> instantiation
swap(cA1, cA2);
return 0;
}
Code above compiles.
In class A, friend void swap<>(A<T>& lhs, A<T>& rhs) which requires a forward declatation seems to behave the same way as friend void swap<>(A& lhs, A& rhs) without the need for a forward declaration.
Can anyone explain, is there a difference between the two except that one requires a forward declaration, and the other does not? In what ways are they different to that effect?

Related

Friend specific template instantiation of operator

I have a class template and an operator template that needs to access its private field. I can make a template friend:
template <typename T>
class A {
int x;
template <typename U>
friend bool operator==(const A<U>& a, const A<U>& b);
};
template <typename T>
bool operator== (const A<T>& a, const A<T>& b) {
return a.x == b.x;
}
int main() {
A<int> x, y;
x == y;
return 0;
}
But is it possible to make only operator==<T> friend for A<T> and not making operator==<int> friend of A<double> ?
If having trouble with friend, then bring the declaration forward, before the A class is defined.
template <typename T>
bool operator== (const A<T>& a, const A<T>& b);
Then you can friend it more clearly. Full solution (ideone):
template <typename T>
class A;
// declare operator== early (requires that A be introduced above)
template <typename T>
bool operator== (const A<T>& a, const A<T>& b);
// define A
template <typename T>
class A {
int x;
// friend the <T> instantiation
friend bool operator==<T>(const A<T>& a, const A<T>& b);
};
// define operator==
template <typename T>
bool operator== (const A<T>& a, const A<T>& b) {
return a.x == b.x;
}
Yes you can. The syntax is as follows:
template <typename T>
class A {
int x;
friend bool operator==<>(const A& a, const A& b);
};
And put your operator== definition(or just a declaration) before the A class.

Operator overload in class templates and friendship

Suppose I have a small class template as below
template<typename T> class SillyClass {
public:
SillyClass(const T val);
SillyClass(const SillyClass& other);
private:
T data;
};
Now I add the following definitions declarations:
template<typename T> class SillyClass {
public:
SillyClass(const T val);
SillyClass(const SillyClass& other);
SillyClass<T> operator+ (const SillyClass& other);
SillyClass<T> operator+ (const T& val);
private:
T data;
};
and now (after I write the appropriate definitions) I can do things like
SillyClass<int> a(1);
SillyClass<int> b(a);
SillyClass<int> c = a + b;
SillyClass<int> d = a + 3;
So far, so good. However, to be able to write something like
SillyClass<int> e = 3 + a;
I found that must add the following declaration to my class
template<typename T2> friend SillyClass<T2> operator+
(const T2& val, const SillyClass<T2>& other);
so it now reads
template<typename T> class SillyClass {
public:
SillyClass(const T val);
SillyClass(const SillyClass& other);
SillyClass<T> operator+ (const SillyClass& other);
SillyClass<T> operator+ (const T& val);
template<typename TT> SillyClass<TT> friend operator+
(const TT& val, const SillyClass<TT>& other);
private:
T data;
};
I didn't find this in a book and I'd like some help to understand what's happening on this last declaration. For instance, with what is my class being befriended here? Why the additional template declaration? What alternatives do I have in terms of different declarations that lead to the same result (being able to perform 3+a, say)?
Thanks.
with what is my class being befriended here? Why the additional template declaration?
The following expression declared inside your class is a friend declaration:
template<typename TT> SillyClass<TT> friend operator+
(const TT& val, const SillyClass<TT>& other);
The above expression declares your class a friend with a family of overloaded operator+s that take as their left-hand-side parameter a type TT and as their right-hand-side an object SillyClass<TT>. Note though that this is a declaration and not a definition. That is, in order for this to work the template overloaded operator+, must be defined.
What alternatives do I have in terms of different declarations that
lead to the same result (being able to perform 3+a, say)?
For binary operators in order for them to be symmetric must be declared outside the class definition as free functions, and in order to have access to the private members of their class parameters you declare them as a friend function of the respective class.
Based on these lines I would go along with the following implementation:
template<typename T> class SillyClass {
T data;
public:
SillyClass(const T val) : data(val) {}
SillyClass(const SillyClass& other) : data(other.data) {}
template<typename T1, typename T2>
friend SillyClass<typename std::common_type<T1, T2>::type>
operator+(SillyClass<T1> const &lsh, SillyClass<T2>& rhs);
template<typename T1, typename T2>
friend SillyClass<typename std::common_type<T1, T2>::type>
operator+(SillyClass<T1> const &lsh, T2 const &rhs);
template<typename T1, typename T2>
friend SillyClass<typename std::common_type<T1, T2>::type>
operator+(T1 const &rhs, SillyClass<T2> const &rsh);
};
template<typename T1, typename T2>
SillyClass<typename std::common_type<T1, T2>::type>
operator+(SillyClass<T1> const &lhs, SillyClass<T2>& rhs) {
return SillyClass<typename std::common_type<T1, T2>::type>(lhs.data + rhs.data);
}
template<typename T1, typename T2>
SillyClass<typename std::common_type<T1, T2>::type>
operator+(SillyClass<T1> const &lhs, T2 const &rhs) {
return SillyClass<typename std::common_type<T1, T2>::type>(lhs.data + rhs);
}
template<typename T1, typename T2>
SillyClass<typename std::common_type<T1, T2>::type>
operator+(T1 const &lhs, SillyClass<T2> const &rhs) {
return SillyClass<typename std::common_type<T1, T2>::type>(rhs.data + lhs);
}
Live Demo
Instead of only declaring your operator in class, you can define the friend operator inside your class:
SillyClass<T> friend operator+(const T& val, const SillyClass<T>& other)
{
// define it here
}
This way, for each instance of your class, a corresponding friend will be generated. The whole thing works based on something called friend name injection.
Now back to your problem. If you just use an in-class declaration
friend operator+(const T& val, const SillyClass<T>& other);
and then outside your class you define
template<typename T>
friend operator+(const T& val, const SillyClass<T>& other)
you'll get a linker error. Why? Because suppose T=int when you instantiate the class. Then the declaration in your class will read as
friend operator+(const int& val, const SillyClass<int>& other);
and will be a better match than the above template definition whenever you invoke your operator+ on SillyClass<int> and some int. So the linker won't be able to find the definition of the required int instantiation, hence a linker error. On the other hand, the solution I proposed at the beginning of the answer guarantees a corresponding definition for every type, so it will work without any headaches.

operator overloading as a friend function

I create a template class and want to overload an operator + (several times). I do this in the following way
template <typename T> class Polynomial;
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar);
....
template <typename T>
class Polynomial {
....
public:
....
Polynomial operator +(const Polynomial& other) const {
// impelementation
}
friend const Polynomial<T> operator + <> (const Polynomial<T>& poly, const T& scalar);
};
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar) {
// implementation
}
However, I got the following error (which corresponds to the line that begins with 'friend')
problem2.cpp:282:45: error: declaration of ‘operator+’ as non-function
problem2.cpp:282:45: error: expected ‘;’ at end of member declaration
problem2.cpp:282:47: error: expected unqualified-id before ‘<’ token
Following Raxvan's advise, I've changed the code
template class Polynomial;
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar);
template <typename T>
ostream& operator <<(ostream& out, const Polynomial<T>& other);
....
template <typename T>
class Polynomial {
....
public:
....
friend ostream& operator << <> (ostream& out, const Polynomial<T>& other);
Polynomial operator +(const Polynomial& other) const {
// impelementation
}
template <typename NOT_T>
friend const Polynomial<NOT_T> operator +(const Polynomial<NOT_T>& poly, const NOT_T& scalar);
};
template <typename T>
ostream& operator <<(ostream& out, const Polynomial<T>& other) {
// implementation
}
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar) {
// implementation
}
And in this code I don't have problems with operator << as I had with operator +. Can anyone explain the defference?
The problem is subtle. Your syntax is very close to being
correct. I think your friend declaration should be:
friend Polynominal<T> const operator+<T>( ... );
but both VC++ and g++ accept:
friend Polynominal<> const operator+<T>( ... );
when they can see the declaration:
template <typename T>
const Polynomial<T> operator+(const Polynomial<T>& poly, const T& scalar);
(I can't find anything in the standard to justify this off hand,
but since both VC++ and g++ do it, I suppose that it's something
I've missed.)
And therein lies the problems. The declaration at namespace
scope of operator+ is hidden by the in-class definition. The
compiler complains because the operator+ it finds (the in
class definition) isn't a template itself (although it is
a member of a class template).
If you don't have the problem with operator<<, it's because
you don't have a member function with the same name.
There are several ways of solving this problem. The simplest is
probably to make all of the operator+ friends. Or not: the
most common approach to this is to implement operator+ in
terms of operator+= (which should be a member, in all cases).
In which case, operator+ doesn't have to be a friend.
Or you don't make operator+ a template at all, but definite it
inside your class template:
template <typename T>
class Polynomial
{
friend Polynomial operator+( Polynomial const& lhs, Polynomial const& rhs )
{
// ...
}
friend Polynomial operator+( Polynomial const& lhs, T const& rhs )
{
// ...
}
friend Polynomial operator+( T const& lhs, Polynomial const& rhs )
{
// ...
}
}
(Note that the functions being defined are not templates, but
separate overloaded non-template functions, one for each
instantiation of Polynomial. But the results end up being the
same.)
In this case, you probably would want to have a member function,
called by the operator+ functions, which would do the actual
work; you don't want too much code directly inline like this.
Your operator+ is a function template, to make this a friend, you need to declare it fully including the template parameters, however with a different template parameter, for example:
#include <iostream>
template <typename T> class Polynomial;
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar);
template <typename T>
class Polynomial {
public:
Polynomial operator +(const Polynomial& other) const {
std::cout << "inline +" << std::endl;
}
// Here introduce a different type to indicate that this is a template...
template <typename U>
friend const Polynomial<U> operator + (const Polynomial<U>& poly, const U& scalar);
};
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar) {
std::cout << "friend +" << std::endl;
}
int main(void)
{
Polynomial<int> f;
f = f + 1;
f = f + 1.; // this fails
f = f + Polynomial<int>();
}
You have to have the same definition when you label a function as friend, this includes the template used above with another type , not T
template <typename T> class Polynomial;
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar);
template <typename T>
class Polynomial {
public:
template < class NOT_T> //must respect the deffinition from above
friend const Polynomial<NOT_T> operator + (const Polynomial<NOT_T>& poly, const NOT_T& scalar);
};
template <typename T>
const Polynomial<T> operator +(const Polynomial<T>& poly, const T& scalar)
{
//...
}
Edit:
For a simplified explanation i have changed the function to foo and bar to illustrate the difference in declarations:
template <typename T> class Polynomial;
template <typename T>
void bar(const Polynomial<T>& );
void foo(const Polynomial<float> & );//foo for speciffic float
template <typename T>
class Polynomial {
public:
template <typename> //this is also valid declaration;
friend void bar(const Polynomial<T> & );
//it just has to have template because it's a template functions declaration
//not a valid declaration:
//friend void bar <> (const Polynomial<T> & );
//this declaration has no template;
//it refers to a foo function specific to Polynomial<T> type
//so if you use Polynomial<float> there must be a foo for floats
friend void foo(const Polynomial<T> &);
};
template <class T>
void bar(const Polynomial<T>&)
{
}
void foo(const Polynomial<float> &)
{
}
void main()
{
Polynomial<float> pf;
Polynomial<int> pi;
foo(pi);//error since there is not **foo** declared for int
foo(pf);//ok; we have **foo** for Polynomial<float>
bar(pf);
bar(pi);//both ok since bar is templated function;
}
Raxvan.

Linker error: Template relational operator

Why does this code give me a linker error and how do I fix it?
Undefined symbols for architecture x86_64: "operator==(foo const&, foo const&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64
template<typename T>
class foo {
//friends get access to the private member t
friend bool operator==(const foo<T> &lhs, const foo<T> &rhs);
T t;
};
template<typename T>
bool operator==(const foo<T> &lhs, const foo<T> &rhs) {
return lhs.t == rhs.t;
}
int main(int,char**) {
foo<int> f1, f2;
if (f1 == f2)
;
return 0;
}
Here is a fix of your code:
template<typename T>
class foo; // Forward declaration
template<typename T> // Need to define or declare this before the class
bool operator==(const foo<T> &lhs, const foo<T> &rhs) {
return lhs.t == rhs.t;
}
template<typename T>
class foo {
// Notice the little <> here denoting a specialization
friend bool operator==<>(const foo<T> &lhs, const foo<T> &rhs);
T t;
};
operator== is a function template, but the friendship declaration doesn't reflect this. This is one way to fix it:
template <class U>
friend bool operator==(const foo<U> &lhs, const foo<U> &rhs);
One very minor glitch is that it gives operator==<int> friend-access to foo<string>. For this reason, I think #JesseGood's fix is cleaner, albeit (paradoxically) more verbose.
You need to specify the template types again, but different to the class template type:
template<typename V>
friend bool operator==(const foo<V> &lhs, const foo<V> &rhs);
When overloading operators, avoid using friend functions; you'll want to define your function as a public class member, and make it take only one argument (not two).
template<typename T>
class foo {
//public function allowing access to the private member t
public:
bool operator==( foo<T> &rhs)
{
return t == rhs.t;
}
private:
T t;
};
int main(int,char**) {
foo<int> f1, f2;
if (f1 == f2)
;
return 0;
}

Relational operators on a class template

This will not work
template<typename T>
struct foo {
T t;
};
bool operator==(const foo &lhs, const foo &rhs) { //error, requires template arg
return lhs.t == rhs.t;
}
Is this the correct way to solve this? I want define also the operators <,>,<=,>=,!= so doing template<typename T> on all of them would be lengthy.
template<typename T>
struct foo {
T t;
};
template<typename T>
bool operator==(const foo<T> &lhs, const foo<T> &rhs) {
return lhs.t == rhs.t;
}
There are two solutions: you can define them as const member functions inside the class
template<typename T>
struct foo {
T t;
bool operator==(const foo &lhs, const foo &rhs) const { return lhs.t == rhs.t; }
// same for the other relational operators
};
This works because inside the class you can use foo as a shorthand for foo<T>.
An alternative is to define them as friend non-member functions inside the class
template<typename T>
class foo {
T t;
friend bool operator==(const foo &lhs, const foo &rhs) const { return lhs.t == rhs.t; }
// same for the other relational operators
};
If you define t as a private member, then you actually need to make operator== a friend function in order to let it gain access. Note however, that this will have the side-effect as injecting them as non-member non-template functions in the surrounding namespace. This has some consequences for argument-dependent name lookup.
if you don't care about implicit conversions, you can set them as member functions, so you won't have to retype it each time.
but if you have to define them as free functions, I'm afraid you don't have a choice.