简体   繁体   中英

Forward declare possible typedef c++0x

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).

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.

A typedef creates another name for some type. When you do

typedef X Z;

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

class X;

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* or char .

C++0x will not change this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM