为什么我不能通过typedef声明一个朋友? [英] Why can't I declare a friend through a typedef?

查看:106
本文介绍了为什么我不能通过typedef声明一个朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道类名的typedef不能像朋友声明的类名一样工作?

Does anyone know why typedefs of class names don't work like class names for the friend declaration?

class A
{
public:
};

class B : public A
{
public:
   typedef A SUPERCLASS;
};

typedef A X;

class C
{
public:
   friend class A;             // OK
   friend class X;             // fails
   friend class B::SUPERCLASS; // fails
};


推荐答案

目前不能。我不知道原因(只是查找它,因为我发现它很有趣)。更新:您可以在第一个建议中找到支持typedef名称作为朋友的原因: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1520.pdf 。原因是,标准只支持精心设计的类型说明符。很容易允许只有那些,并说如果实体声明为朋友还没有声明,它将成为周围的命名空间的成员。但这意味着如果你想使用一个模板参数,你必须做(例如需要一个类)

It can't, currently. I don't know the reason yet (just looking it up, because i find it interesting). Update: you can find the reason in the first proposal to support typedef-names as friends: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1520.pdf . The reason is that the Standard only supported elaborated-type-specifiers. It's easy to allow only those, and say if the entity declared as friend is not declared yet, it will be made a member of the surrounding namespace. But this means that if you want to use a template parameter, you would have to do (a class is required then for example)

friend class T;

但这带来了额外的问题,它被认为不值得的收益。现在,本文提出允许给出附加的类型说明符(以便允许使用模板参数和typedef名称)。

But that brought additional problems, and it was figured not worth the gain. Now, the paper proposes to allow additional type specifiers to be given (so that this then allows use of template parameters and typedef-names).

下一个C ++版本到2010)将能够做到。

The next C++ version (due to 2010) will be able to do it.

查看此标准更新的提案: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1791.pdf 。它不会只允许typedef名称,而且也不能使用模板参数作为声明为friend的类型。

See this updated proposal to the standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1791.pdf . It will not allow only typedef names, but also template parameters to be used as the type declared as friend.

这篇关于为什么我不能通过typedef声明一个朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆