前向声明可能的 typedef c++0x
我从问题的答案中了解到在c ++中转发声明类的公共typedef ,在 C++ 中向前声明可能是 typedef 的东西是不可能的。
是否可以在 C++0x 中执行此问题所要求的操作?
否则,进行如下更改
class X {...};
typedef X Z;
会
class Y {...};
typedef Y Z;
破坏客户端代码。
我认为情况不应该如此,因为 typedef 的要点是它们应该使底层类型对客户端透明,因此您可以在不破坏客户端代码的情况下更改实现。
澄清
基本上,可以说我们可以有这两个选项:
class X {...};
typedef X Z; // (1)
或者
class Z {...}; // (2)
我希望能够在客户端代码中执行此操作:
class Z; // Or something of this effect, sadly this fails in the case of (1)
并且无论 Z 是 typedef 还是类,该代码都不需要更改(这确实应该对客户透明)。
I understand from the answer to the question Forward declare a class's public typedef in c++, forward declaring something which may be typedef is impossible in C++.
Is it possible to do what this question asks in C++0x?
Otherwise, making changes like:
class X {...};
typedef X Z;
to
class Y {...};
typedef Y Z;
breaks client code.
I think it shouldn't be the case, because the point of typedefs is that they're supposed to make the underlying type transparent to the client, so you can change the implementation without breaking client code.
Clarification
Basically, lets say we have could have these two options:
class X {...};
typedef X Z; // (1)
OR
class Z {...}; // (2)
I want to be able in client code to do this:
class Z; // Or something of this effect, sadly this fails in the case of (1)
And that code not needing to change no matter whether Z is a typedef or a class (which really should be transparent to the client).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设标头包含在用户源的“头部”,并且它们不会转发声明您的类。如果用户想要一个更轻量级的标头,就给他们一个。
标准库包括一个由前向声明
组成的标头。您可以采用或调整该约定,例如"foo_forward.h"
。当用户在任何地方为您的类编写前向声明时,他实际上是在为您编写标头。这是一个站不住脚的安排。
Assume that headers are included at the "head" of the user's sources and that they won't forward declare your classes. If users want a lighter-weight header, give them one.
The Standard Library includes one header consisting of forward declarations,
<iosfwd>
. You might adopt or adapt that convention, for example"foo_forward.h"
.When the user writes a forward declaration for your class, anywhere, he is essentially writing your header for you. That is an untenable arrangement.
typedef 为某种类型创建另一个名称。当你这样做时,
你告诉编译器“Z 是 X 的另一个名字”。如果此时它不知道 X 是什么,那么该信息就毫无用处。
当你说
编译器至少知道 X 是用户定义的类型时,这很有帮助。然后它可以排除 X 不是可能需要特殊处理的类型之一,例如
void*
或char
。C++0x 不会改变这一点。
A typedef creates another name for some type. When you do
you tell the compiler "Z is another name for X". If it doesn't know what X is at that point, that info is pretty useless.
When you say
the compiler at least knows that X is a user defined type, which is helpful. It can then rule out that X isn't one of the types that could need special treatment, like
void*
orchar
.C++0x will not change this.