Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Is it possible to friend all instances of a template class?

Tags:

c++

templates

I have a template class called RenderShader, I also have a class called Render. I would like to have all instances of the RenderShader template class to be friends with Render. Also the user is allowed to make their own RenderShader instances so it's not possible to list all of them in the main header file. For Example:

template<class vertdef>
class RenderShader
{
public:
.....
};

class Render
{
    friend class RenderShader;
public:
    ....
};

The syntax "friend RenderShader" is wrong, is there a correct syntax for what I would like to do?

like image 229
KPexEA Avatar asked Dec 19 '11 23:12

KPexEA


People also ask

Can a template class be a friend?

Many-to-one: All instantiations of a template function may be friends to a regular non-template class. One-to-one: A template function instantiated with one set of template arguments may be a friend to one template class instantiated with the same set of template arguments.

Can there be more than one argument to template?

Can there be more than one argument to templates? Yes, like normal parameters, we can pass more than one data type as arguments to templates.

How many times is the template class instantiation?

Template instantiation has two forms: explicit instantiation and implicit instantiation.

Can you inherit template class?

It is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.


1 Answers

class Render
{
    template<class vertdef>
    friend class RenderShader;
public:
    ....
};
like image 141
Johannes Schaub - litb Avatar answered Oct 12 '22 22:10

Johannes Schaub - litb