开发者

Why typedef can not be used with static?

Why typedef can not开发者_JAVA技巧 be used with static? For example, the code below is an error

typedef static int INT2;

What other rules should be follow to use the typedef? What other keywords can not be used with typedef?

Thanks so much!


typedef doesn't declare an instance of a variable, it declares a type (type alias actually),

static is a qualifier you apply to an instance, not a type, so you can use static when you use the type, but not when you define the type. Like this..

typedef int int32;
static int32 foo;


The static keyword is not part of the type, depending on the context it is a storage or scope specifier and has no influence whatsoever on the type. Therefore, it cannot be used as part of the type definition, which is why it is invalid here.

A typedef is a type definition, i.e. you're saying 'this name' now refers to 'this type', the name you give must be an identifier as defined by the language standard, the type has to be a type specifier, i.e. an already named type, either base type or typedef'd, a struct, union, class, or enum specifier, with possible type qualifiers, i.e. const, or volatile.

The static keyword however does not change the type, it says something about the object, (in general, not in the OOP sense.) e.g. it is the variable that is placed in static storage, not the type.

It looks like you're trying to use a typedef as a macro, i.e.

#define MYINT static int


Storage duration is associated with objects. A typedef declaration creates an alias -- a new name for a type. It does not create objects. Hence you cannot use a storage specifier with a typedef.


typedef cannot be used along with static as both typedef and static are storage classes. If you define a variable as typedef static int a; then there exist multiple storage classes for the variable a.

Storage classes are used to define the scope (visibility) and life-time of variables and/or functions. Some examples are static, auto, register, extern, typedef etc.


As many other people have mentioned, static is a storage class specifier, not a type. What this means, specifically, is that the static keyword tells tells the compiler something about where a variable should be placed, whether or not it should appear as a symbol for external linking, or how long the variable should remain in existence.

A type is an interpretation of a memory location. It's a description of what kind of data lives in that location and comes associated with a set of operations that can be performed on that data.

So type and storage class are only related in that they both say something about a piece of data. The typedef keyword is for creating new names for types.

As for a random analogy...

A type is like talking about a specific make and model of car.

A storage class is like saying that a car is stored in a private heated garage and only used on a private racetrack.

A typedef is a nickname for a specific make and model.


I think the answers from wich and Bailey are correct and enough, but to help clarify to those that commented: When you declare a typedef the resulting type must be consistent everywhere it can be used. What if the typedef you created appears in the following code:

INT2 myfunction()
{
  return 0;
}

class MyClass
{
  public: INT2 x;
};

INT2 MyClass::x;
  • The function returning INT2 would make no sense. Of course the return value is not static (it can't be).
  • The x member declaration in MyClass would be static, and also its later definition? That's not only redundant, is incorrect, because you don't use the static keyword when defining a static member - only when declaring it.

The same applies to the other keywords mentioned by Bailey. Think about it: the only keywords that can declare consistently a type for all its uses are the primitive types themselves, and the type modifiers (const, unsigned, etc.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜