Ad N
Ad N

Reputation: 8396

Is it possible to mark an alias template as a friend?

Imagine we have this code:

template <class, class>
class Element
{};

template <class T>
class Util
{
public:
   template <class U>
   using BeFriend = Element<T, U>;
};

Is it possible to mark BeFriend as a friend ? (Of Util, or any other class).

 Edit

The "obvious" syntax were tried, but both failed with Clang 3.6.

template <class> friend class BeFriend;
template <class> friend BeFriend;

I did not know about the second syntax, but found it in this answer. It seems to work (and be required) for non-template aliases, but does not help in this case where the alias is templated.

(Note: as some could infer from the minimal example, I am looking for a way to work-around the limitation that C++ does not allow to friend a partial template specialization)

Upvotes: 13

Views: 1749

Answers (2)

songyuanyao
songyuanyao

Reputation: 172994

I think you can't do that because partial specializations could not be declared as friend.

From the stardard, [temp.friend]/7

Friend declarations shall not declare partial specializations. [ Example:

template<class T> class A { };
class X {
  template<class T> friend class A<T*>; // error
};

—end example ]

You have to specify a more generic version such as:

template <class, class> friend class Element;

or a full specified version, such as:

using BeFriend = Element<T, int>;
friend BeFriend;

Upvotes: 9

sergej
sergej

Reputation: 18009

The problem is not the alias, the problem is that "partial specialization cannot be declared as a friend".

template <class, class> friend class Element;        // OK

template <class, class> friend class Element<T, T>;  // Error

Upvotes: 2

Related Questions