是否可以将别名模板标记为朋友? [英] Is it possible to mark an alias template as a friend?
问题描述
假设我们有以下代码:
template <class, class>
class Element
{};
template <class T>
class Util
{
public:
template <class U>
using BeFriend = Element<T, U>;
};
是否可以将BeFriend
标记为朋友? (属于Util
或任何其他类).
Is it possible to mark BeFriend
as a friend ? (Of Util
, or any other class).
显而易见"的曾尝试使用语法,但都因Clang 3.6失败.
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.
(注意:正如一些可以从最小示例中推断出的,我正在寻找一种方法来解决C ++不允许将部分模板专业化作为朋友的局限性)
(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)
推荐答案
我认为您不能这样做,因为无法将部分专业化声明为好友.
I think you can't do that because partial specializations could not be declared as friend.
从标准禽中, [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
};
-结束示例]
您必须指定一个更通用的版本,例如:
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;
这篇关于是否可以将别名模板标记为朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!