Question

How to declare a variadic template function as a friend?

For example as follows:

template<class T>
class A
{
    friend ??? MakeA ??? ; // What should be placed here ???

    A(T)
    {}
};

template<class T, class... Args>
A<T> MakeA(Args&&... args)
{
    T t(std::forward<Args>(args));

    return A(t);
}
Was it helpful?

Solution

It's quite straightforward. It's simply a template declaration with the added friend specifier:

template<class T>
class A
{
    template<class T1, class... Args>
    friend A<T1> MakeA(Args&&... args);

    A(T) { }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top