无法与 typedef 交好:有什么特殊原因吗?
struct A {};
typedef A B;
struct C { friend struct B; };
GCC 4.7.0 20110427 告诉我错误:在“struct”之后使用 typedef-name“B”
。
到目前为止,这似乎是不言自明的。毕竟,我的示例代码试图声明并友交一个名为 B 的struct
,它实际上不是一个struct-key
。
然而,我必须写 friend struct A;
如果 A
实际上是一个复杂、冗长的模板元黑客混乱,这是不可取的。
我是否遗漏了一些东西,或者我们实际上可以通过类型别名不 friend
类型?如果不是,是否有任何特殊原因或者只是语言的怪癖?
这个问题之前曾提出过这个问题,但现在过时了,并对有关 C++0x 的问题做出了看似不正确的断言。 这个问题涉及 C++0x FDIS。
struct A {};
typedef A B;
struct C { friend struct B; };
GCC 4.7.0 20110427 tells me error: using typedef-name 'B' after 'struct'
.
So far, this seems pretty self-explanatory; after all, my example code is trying to declare-and-friend a struct
called B, which is in fact not a struct-key
.
However, I have to write friend struct A;
if A
is in fact a complex, long-winded mess of template metahackery, this is not desirable.
Am I missing something, or can we in fact not friend
types through type aliases? If not, is there any particular reason or is it just a quirk of the language?
This question brought up the issue before, but is dated and makes assertions on the matter regarding C++0x that don't appear to be true. This question instead regards the C++0x FDIS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以与任意类型交友(对于非类类型,交友声明将被忽略),但是你应该省略 struct :
You can befriend arbitrary types (for non-class types, the friend declaration will be ignored), but then you shall omit
struct
: