Go ahead declare a possible typedef C ++ 0x

As far as I understand from the answer to the question Forward declaration of public public typedef in C ++ , forward the declaration of what may be typedef impossible in C ++.

Is it possible to do what this question asks in C ++ 0x?

Otherwise, the following changes are made:

class X {...}; typedef XZ; 

to

 class Y {...}; typedef YZ; 

breaks client code.

I think this should not be the case, because the point of typedefs is that they should make the base type transparent to the client, so you can change the implementation without breaking the client code.

Explanation

In principle, let's say we can have these two options:

 class X {...}; typedef XZ; // (1) 

OR

 class Z {...}; // (2) 

I want to be able to do this in client code:

 class Z; // Or something of this effect, sadly this fails in the case of (1) 

And this code does not need to be changed regardless of whether Z is a type or a class (which really should be transparent to the client).

+4
source share
2 answers

Suppose headers are included in the "head" from user sources and that they will not redirect the declarations of your classes. If users need a lighter weight header, give them one.

The standard library includes one header, consisting of forward declarations, <iosfwd> . You can accept or adapt this agreement, for example, "foo_forward.h" .

When a user writes an ad ahead for your class, anywhere, he essentially writes your headline for you. This is a failed arrangement.

+1
source

The typedef command creates a different name for some type. When you do

 typedef XZ; 

you tell the compiler "Z is another name for X." If he does not know what X is at this point, this information is pretty useless.

When you speak

 class X; 

the compiler at least knows that X is a user-defined type, which is useful. He can then rule out that X is not one of the types that might need special handling, such as void* or char .

C ++ 0x will not change this.

+1
source

Source: https://habr.com/ru/post/1346337/


All Articles