Cannot understand friend functions in a template class - c++

This is code i have written to understand the concept. The code is fine and it runs.
What i dont understand is that why is the marked line needed ?
template <class T>
class D
{
public :
template <class P> //<------------------Why is this needed ? --------------
friend void print(D <P> obj);
};
template <class T>
void print(D<T> obj)
{std::cout<<sizeof(T);};
int main()
{
D <char>obj3;
print(obj3);
return 0;
}
or in other words why does the following not run ?
template <class T>
class D
{
public :
friend void print(D <T> obj);
};

As per [temp.friend], you must provide explicit template arguments to make a specialisation of a template function a friend:
template <class T>
class D
{
public :
friend void print<T>(D <T> obj);
};
Without it, the compiler will be looking for a function print(), not a function template print().

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.

friend function using template classes c++

I made a class which has a friend function and when I declare it there are no problems but, when I write its code it returns me the error:
"Out-of-line definition of 'change' does not match any declaration in 'MyClass '".
Here's the code
template <class T>
class MyClass {
private:
T a;
public:
MyClass(T);
~MyClass();
friend void change(MyClass);
};
template <class T>
MyClass <T> :: MyClass(T value) {
a = value;
}
template <class T>
MyClass <T> :: ~MyClass() {}
template <class T>
void MyClass <T> :: change(MyClass class) { //Out-of-line definition of 'change' does not match any declaration in 'MyClass <T>'
a = class.a;
}
friend void change(MyClass); does not declare a member function of MyClass, it is an instruction for the compiler to grant the free function¹ void change(MyClass); access to private/protected members of MyClass.
The free function you grant access to MyClass would then have to look that way:
template <class S>
void change(MyClass<S> obj) {
obj.a; // obj.a can be accessed by the free function
}
But the friend then has to be declared that way in the class:
template <class T>
class MyClass {
private:
T a;
public:
MyClass(T);
~MyClass();
template <class S>
friend void change(MyClass<S>);
};
But based on a = class.a in change I think you actually want to have this:
template <class T>
class MyClass {
private:
T a;
public:
MyClass(T);
~MyClass();
void change(MyClass);
};
template <class T>
MyClass <T> :: MyClass(T value) {
a = value;
}
template <class T>
MyClass <T> :: ~MyClass() {}
template <class T>
void MyClass <T>::change(MyClass class) {
a = class.a;
}
A member function of MyClass can always access all members of any instance of MyClass.
1: What is the meaning of the term “free function” in C++?

specializing template member function to work in a different way for a special template class

I have two classes class A and class B both of them are template classes for a member function in A I want it to act in a special way when the type of A is B
and in a normal way for any other types I don't know how to do this ?
template <class B>
class B
{
private:
T m;
public:
...... any member functions
}
template <class T>
class A
{
private:
T var;
public:
void doSomething();
};
template <class T>
void A<T>::doSomething(){...........//implementation}
template <class T>
void A<B<T>>::doSomething(){................//different implementation}
You can specialize A this way:
template <class T>
class A<B<T>> {
// ...
};
This is an instance of partial template specialization.
If you refuse to specialize the entire class, you can defer the work from A<T>::doSomething() to a function doSomethingForA<T>(A &) that would be partially specialized, and that would possibly be friend of A<T>.
Hope this solves your problem:
#include <iostream>
template <typename T>
struct B {};
template <typename T> struct A;
template <typename T>
void doSomething(T&) { std::cout << "General\n"; }
template <typename T>
void doSomething(A<B<T>>&) { std::cout << "Special\n"; }
template <typename T>
struct A {
void doSomething() {
::doSomething(*this);
}
};
int main()
{
A<int> general;
A<B<int>> special;
general.doSomething();
special.doSomething();
}

What must be done for a static template function defined in a nested class to be declared a friend in a sibling nested class?

Using GCC 4.8.2 on Linux, I want to grant the factory method Create() access to the private constructor of class C, but I get "error: 'Create' was not declared in this scope" when attempting to declare a specialized friend. How do I get this to work without opening up the declaration to all types for B::Create()?
template <typename T> class A {
public:
class B;
template <typename U> class C;
};
template <typename T>
class A<T>::B {
public:
template <typename U> static void Create();
};
template <typename T> template <typename U>
class A<T>::C {
C() = default;
friend void B::Create<U>();
};
template <typename T> template <typename U>
void A<T>::B::Create() {
C<U>{};
}
int main() {
A<int>::B::Create<char>();
}
I think you have run into a compiler defect. The following code, that uses only one layer of templates, works fine in g++ 4.8.2.
class Foo
{
public:
template <typename U> static Foo* Create();
};
template <typename U> class Bar : public Foo
{
private:
Bar() = default;
friend Foo* Foo::Create<U>();
};
template <typename U>
Foo* Foo::Create() {
return new Bar<U>();
}
However, the same compiler fails to compile your code. One work around, which I think you have already tried, is to replace
friend B* B::Create<U>();
with
template <typename V>
friend B* B::Create<V>();
I just tested your code in MSVC++ and it was failing there too. I was able to fix it by just giving the friend declaration a more explicit name. Remember, you can provide fully qualified names anywhere!
I just replaced
friend B* B::Create<U>();
with
friend B* A<T>::B::Create<U>();
I'll fire up MinGW and see if it actually does what you expect it to do.

friend template function of template class

I have the following simplified code:
template <class T>
class A
{
public:
template <class U>
static U foo(T* p)
{
p;
return U();
}
};
class B
{
/*template <class T>
template <class U>
friend U A<T>::foo<U>(T*);*/
friend B A<B>::foo<B>(B*);
B()
{}
public:
};
...
A<B>::foo<B>(nullptr);
And it works quite well. But the things that I've not managed to do are commented:
/*template <class T>
template <class U>
friend U A<T>::foo<U>(T*);*/
I don't know what the syntax I should use to make it works. So I need to generalize my friend declaration to all possible types. I've tried quite a few variants of syntax but had no success. Could some one point me out what should I write instead of my commented code to make it works?
Thanks!
What you're looking for is
template <class T>
template <class U>
friend U A<T>::foo(T*);
The following works on IdeOne.com
#include <iostream>
template <class T>
class A
{
public:
template <class U>
static U foo(T* p)
{
p;
return U();
}
};
class B
{
template <class T>
template <class U>
friend U A<T>::foo(T*);
B() {}
public:
void hello() const
{
std::cout << "I'm a B!" << std::endl;
}
};
int main(int, char*[])
{
A<B>::foo<B>(NULL).hello();
}
I will agree with the othe commenters, friends and templates simply don't mix, at least not in any consistent way with across a range of compilers. The standard probably states exactly what should work, though that will likely not help you. Sad but true.
Friend is often not a good idea anyhow, it might be better to consider how to write the code without friendship, or use the "poor man's friend" of a public method with a comment indicating it is not for general use.