Why can't I post typedefs declaration?

namespace O { class A{}; class A; // ok typedef AK; // ok struct A; // ok(C++11): A is a class but for references and pointer it have the same meaning class K; // (1) error: K is a typedef (of a class...) } namespace U { typedef O::AA; class A; // (2) error: A is a typedef (of a class...) } 

What is the reason the C ++ standard does not allow these cases (1 and 2) to compile?

+7
source share
3 answers

You are confused, or your example does not show what you are asking. In your code example, you are not trying to "forward the typedef declaration" (for example, this is not possible or useful, see below), you are trying to update the existing typedef name (i.e., an Alias ​​for one type) as a completely different type.

You already said that K is a typedef for A , then you say it is a class K Think about it. It cannot be both class A and class K Both that, and another (1) and (2) do not work for the same reason.

Looking through these lines of the example:

 class A; // ok typedef AK; // ok 

Still.

 struct A; // ok(C++11): A is a class but for references and pointer it have the same meaning 

I don’t know why you said "C ++ 11" here, it is normal in C ++ 03. Classes and structures are the same in C ++. They are "object types" and "class types." For a direct declaration, the class key (i.e., struct or class ) is interchangeable.

 class K; // (1) error: K is a typedef (of a class...) 

K declared as typedef for class A , the name cannot be reused to declare a new type in the same scope.

[In addition, C resolves the following because structure names and typedef names are in different namespaces:

 struct A { }; typedef struct AK; // NB need "struct A" not just "A" struct K { }; 

But now there are two different types, called struct K and K , that are not related to each other. Doing this would be confusing and rather stupid.]

But from your comments, perhaps this is not what you are actually trying to do anyway.

Based on your comments, maybe your broken examples are misleading and what you really want to do:

 typedef class AK; // forward declare A as class and declare K as typedef for it 

This declares a typedef for a type that is not yet defined.

It would be useless to send-declare typedef, you could not do anything about it, because you would not know what type it was typedef, and you can do very little in C ++ without knowing something of type. Not knowing if this is an object type, a link type, or a function type, what you can really do is declare another typedef for it!

Consider:

 typedef K; // declares K to be a typedef K* f(); // function returning pointer to K void g(K*); // function taking pointer to K 

I think you say you want this to be true, so do you expect this to work?

 K* k = f(); g(k); 

That should work, right? You don't need to know type K because you only pass it to pointers, right? Wrong. What if you later define K as follows:

 typedef int& K; 

Now f has a signature int&* f() , which is invalid. You should know what typedef is for typedef, so its declaration should state that it is not just forward-declaring it as a name.

+14
source

Let me solve the first error:

  • You create class A: at this moment, this class is given the name "A"; no other objects in this area can be named "A".
  • Then you type so that K now refers to A, so no other objects in this area can be named "K".
  • Then you try to send the K. Forward declaration declaration yourself, but the name K is already accepted by your typedef. At the moment, this has nothing to do with A. You cannot forward ad A either, both names are already taken by your previous use.

What are you trying to do anyway?

+4
source

I cannot find this thing in the C ++ 2003 standard. But the C compiler does not allow this, because the typedef construct defines a new type, and then you try to define a new type again through class A.

In principle, the use of a “declaration class” was allowed for two use cases:

  • Bureye names
  • structure pointer

Both operations do not require information about the sizeof type and its memory layout. Also in the list above there are no "links", because in C. there are no links.

See also: (Samuel P. Harbison, Guy L. Stale] CA Reference, p. 151.

0
source

All Articles