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

折月煮酒 提交于 2019-12-06 19:56:28

问题


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)


回答1:


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;



回答2:


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


来源:https://stackoverflow.com/questions/33563427/is-it-possible-to-mark-an-alias-template-as-a-friend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!