How to use friend keyword for template class - c++

lets say that I have 2 template classes, A and B. If I want to make B a friend of A, what would I say ?
class<template T>
class A
{
public:
friend class B<T>; // ???
};
class<template T>
class B
{
};

To use a symbol, it must be declared or defined, this is the same in template. You need to forward declare template B. Also your syntax(class<template T>) to declare template class is not valid, it should be template <class T>.
This should work:
template <typename T> // typename can be replaced with class
class B;
template <typename T>
class A
{
public:
friend class B<T>;
};
template <typename T>
class B
{
};

Related

In C++ can one friend a template class in a non template class for ALL specializations of the template class? [duplicate]

Let's say I'm creating a class for a binary tree, BT, and I have a class which describes an element of the tree, BE, something like
template<class T> class BE {
T *data;
BE *l, *r;
public:
...
template<class U> friend class BT;
};
template<class T> class BT {
BE<T> *root;
public:
...
private:
...
};
This appears to work; however I have questions about what's going on underneath.
I originally tried to declare the friend as
template<class T> friend class BT;
however it appears necessary to use U (or something other than T) here, why is this? Does it imply that any particular BT is friend to any particular BE class?
The IBM page on templates and friends has examples of different type of friend relationships for functions but not classes (and guessing a syntax hasn't converged on the solution yet). I would prefer to understand how to get the specifications correct for the type of friend relationship I wish to define.
template<class T> class BE{
template<class T> friend class BT;
};
Is not allowed because template parameters cannot shadow each other. Nested templates must have different template parameter names.
template<typename T>
struct foo {
template<typename U>
friend class bar;
};
This means that bar is a friend of foo regardless of bar's template arguments. bar<char>, bar<int>, bar<float>, and any other bar would be friends of foo<char>.
template<typename T>
struct foo {
friend class bar<T>;
};
This means that bar is a friend of foo when bar's template argument matches foo's. Only bar<char> would be a friend of foo<char>.
In your case, friend class bar<T>; should be sufficient.
In order to befriend another same-type struct:
#include <iostream>
template<typename T_>
struct Foo
{
// Without this next line source.value_ later would be inaccessible.
template<typename> friend struct Foo;
Foo(T_ value) : value_(value) {}
template <typename AltT>
void display(AltT &&source) const
{
std::cout << "My value is " << value_ << " and my friend's value is " << source.value_ << ".\n";
}
protected:
T_ value_;
};
int main()
{
Foo<int> foo1(5);
Foo<std::string> foo2("banana");
foo1.display(foo2);
return 0;
}
With the output as follows:
My value is 5 and my friend's value is banana.
In template<typename> friend struct Foo; you shouldn't write T after typename/class otherwise it will cause a template param shadowing error.
It's not necessary to name the parameters so you get fewer points of failure if refactoring:
template <typename _KeyT, typename _ValueT> class hash_map_iterator{
template <typename, typename, int> friend class hash_map;
...
The best way to make a template class a friend of a template class is the following:
#include <iostream>
using namespace std;
template<typename T>
class B;
template<typename T>
class A
{
friend class B<T>;
private:
int height;
public:
A()//constructor
A(T val) //overloaded constructor
};
template<typename T>
class B
{
private:
...
public:
B()//constructor
B(T val) //overloaded constructor
};
In my case this solution works correctly:
template <typename T>
class DerivedClass1 : public BaseClass1 {
template<class T> friend class DerivedClass2;
private:
int a;
};
template <typename T>
class DerivedClass2 : public BaseClass1 {
void method() { this->i;}
};
I hope it will be helpful.

Make all derived template classes friend of other class in C++

Let's say I have to following hierarchy:
template<class T> class Base {
protected:
T container;
};
template<class T> class Derived1 : public Base<T> {
public:
void f1() {
/* Does stuff with Base<T>::container */
}
};
template<class T> class Derived2 : public Base<T> {
public:
void f2() {
/* Does stuff with Base<T>::container */
}
};
Now I want an independent Class (not derived from Base) that can access the Base<T>::container directly from Base or any Derived class. I read about template friend classes and it seems to be the solution to my problem but I couldn't figure out the syntax yet.
I am looking for something like:
template<class T> class Foo{
template<T> friend class Base<T>; // <-- this does not work
public:
size_t bar(const Base<T> &b) const{
return b.container.size();
}
};
Derived1<std::vector<int> > d;
d.f1();
Foo<std::vector<int> > foo;
size_t s = foo.bar()
The friend class line causes an error: specialization of ‘template<class T> class Base’ must appear at namespace scope template<T> friend class Base<T> and the member variable container is still not accessible.
A couple of issues:
Just as you don't put a <T> after the class name when defining a class template:
template <class T> class X<T> { ... }; // WRONG
template <class T> class X { ... }; // RIGHT
you shouldn't put it after the class name when declaring a class template, whether in a forward declaration or friend declaration:
template <class T> class X<T>; // WRONG
template <class T> class X; // RIGHT - declares the template exists
template <class T> friend class X<T>; // WRONG
template <class T> friend class X; // RIGHT - says all specializations of X are friends
(Unless you're creating a class template partial specialization. For example, if class template X is already declared, then template <class T> class X<T*> { ... }; defines a partial specialization that will be used instead of the primary template when the template argument is a pointer.)
And you have the friend declaration backwards: it needs to appear inside the class with the non-public member(s), and name the other class(es) which are allowed to use the members. (If it worked the other way around, then any new class could gain access to any other class's private and protected members, without the permission of the owning class!)
So you would need:
template <class T> class Foo;
template<class T> class Base {
template <class U> friend class Foo;
protected:
T container;
};
The forward declaration of Foo is sometimes not needed, but I think it makes things clearer, and it can avoid gotchas when things start getting more complicated with namespaces, nested classes, etc.
Only Base can say Foo is its friend.
template<typename T> friend class Foo; // every Foo<T> is a friend of Base

Specialization of a class template with the given class

I have a class A and a class template B declared as follows:
class A;
template <class T>
class B;
I want to declare a specialization of B for T=int, which coincides with A, i.e. something like this
template<>
class B<int> = A;
You can emulate such behavior of B via nested classes and C++11 template aliases:
class A;
template <class T>
struct B_
{
class type{ /* Implement your general version of B here */ };
};
template <>
struct B_<int>
{
using type = A;
};
template <class T>
using B = typename B_<T>::type;
Live demo

Specialization of a class that uses Policy classes

I wrote the following:
//Policy Classes
template
<typename T>
struct CheckForZeroSpeed
{
};
template
<typename T>
struct NoCheck
{
};
//Specialization for one of the Policy Classes
template
<>
struct NoCheck<float>
{
};
//Class that uses Policy
template
<typename T,template <class C> class Policy>
class Base : public Policy<T>
{
};
//Inherited class that uses Policy
template
<typename T,template <class C> class Policy>
class Derived : public Base<T,Policy>
{
};
How can I declare a specialization of the Base class (let's say for the int type) and a specialization for the Derived class?
This also works:
template
<>
class Base<int,NoCheck>
{
};
But this is a specialization based both on the type and the policy, is it a way to have specialization based only one (type) of the two template parameters? Something like:
//not compile
template
<template <class C> class Policy>
class Base<int,Policy<T>>
{
};
Edit:
The correct is:
//Partial specialization based on the type
template
<template <class C> class Policy>
class Base<int,Policy>
{
};
//Partial specialization based on the Policy
template
<typename T>
class Base<T,NoCheck>
{
};

Trying to understand the "template <class U> friend class B;" syntax

The following is a simple c++ program that compiles with my MinGW compiler, and executes as expected:
#include <iostream>
template <class T> class A {
T a;
template <class U> friend class B;
public:
A<T> (T t) : a(t) {}
};
template <class T> class B {
A<T> aa;
public:
B<T> (T t) : aa(t) {}
T getT() const {return aa.a;}
};
int main() {
B<int> b(5);
std::cout << "> " << b.getT() << std::endl;
}
Since B<T>::getT() accesses the private A<T>::a member, A<T> makes B<T> a friend with template <class U> friend class B; line.
Unfortunately, I don't know why this line needs to be written like this. Intuitively, I would have expected something like friend class B<T>, yet, this doesn't compile.
The meaning of the newly introduced U is unclear as well, since A's and B's dependant type is T in both cases.
So, in short, I'd appreciate any light on how the syntax for this line is derived or deduced.
There are many different permutations of friendship and templates.
Your present code makes any template specialization of B into a friend for A<T>, so for example B<char> is a friend of A<int>.
If you only wanted to make the matching A<T> a friend, you would say it like this:
template <typename> class B; // forward declare above!
template <typename T>
class A
{
// ...
friend class B<T>;
};
IMHO, friend class B<T>; would have worked had you inserted a forward declaration
template<class T> class B;
before that of class A<T>.
The template <class U> friend class B; makes every class B<U> a friend, not just class B<T>.