可以在声明中的何处放置存储类说明符? [英] Where in a declaration may a storage class specifier be placed?

查看:157
本文介绍了可以在声明中的何处放置存储类说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,让我们考虑static存储类说明符.以下是此存储类说明符有效和格式错误的用法的几个示例:

For example, let's consider the static storage class specifier. Here are a few examples of both valid and ill-formed uses of this storage class specifier:

static int a;        // valid
int static b;        // valid

static int* c;       // valid
int static* d;       // valid
int* static e;       // ill-formed

static int const* f; // valid
int static const* g; // valid
int const static* h; // valid
int const* static i; // ill-formed

typedef int* pointer;
static pointer j;    // valid
pointer static k;    // valid

(Visual C ++ 2012,g ++ 4.7.2和Clang ++ 3.1接受了标记为有效"的声明.所有这些编译器都拒绝了标记为病态"的声明.)

(The declarations marked "valid" were accepted by Visual C++ 2012, g++ 4.7.2, and Clang++ 3.1. The declarations marked "ill-formed" were rejected by all of those compilers.)

这似乎很奇怪,因为存储类说明符适用于声明的变量. static是声明的变量,而不是声明的变量的类型.为什么ei格式错误,而k格式正确?

This seems odd because the storage class specifier applies to the declared variable. It is the declared variable that is static, not the type of the declared variable. Why are e and i ill-formed, but k is well-formed?

管理存储类说明符有效放置的规则是什么?尽管在本示例中使用了static,但该问题适用于所有存储类说明符.最好,完整的答案应引用C ++ 11语言标准的相关部分并进行解释.

What are the rules that govern valid placement of storage class specifiers? While I've used static in this example, the question applies to all storage class specifiers. Preferably, a complete answer should cite relevant sections of the C++11 language standard and explain them.

推荐答案

总之,在声明说明符中的任何位置(请参见ISO/IEC 14882-2012中的7.1节),即*之前. *之后的限定符与指针声明符而不是类型说明符关联,并且static在指针声明符的上下文中没有意义.

In summary, anywhere in the declaration specifier (See section 7.1 in the ISO/IEC 14882-2012), ie before the *. Qualifiers after the * are associated with the pointer declarator, not the type specifier, and static doesn't make sense within the context of a pointer declarator.

请考虑以下情况: 您可以在同一声明列表中声明一个普通的int和一个指向int的指针,如下所示:

Consider the following cases: You can declare a normal int and a pointer to an int in the same declaration list, like this:

int a, *b;

这是因为类型说明符为int,所以您有两个使用该类型说明符inta的声明和一个指针声明程序*a(它声明了指向int的指针).现在考虑:

this is because the type specifier is int, then you have two declarations using that type specifier int, a, and a pointer declarator *a which declares a pointer to int. Now consider:

int a, static b;  // error
int a, *static b; // error
int a, static *b; // error

看起来应该是错误的(实际上),原因(在7.1和8.1节中定义)是因为C和C ++要求您的存储说明符与类型说明符一起使用,而不是在声明符中. 因此,现在应该清楚,以下内容也是错误的,因为上述三个内容也是错误的:

which should look wrong (as they are), and the reason (as defined in sections 7.1 and 8.1) is because C and C++ require that your storage specifiers go with your type specifier, not in your declarator. So now it should be clear that that the following is also wrong, since the above three are also wrong:

int *static a; // error


您的最后一个示例


Your last example,

typedef int* pointer;
static pointer j;    // valid
pointer static k;    // valid

既有效又等效,因为pointer类型定义为类型说明符,并且您可以按任何顺序放置类型说明符和存储说明.请注意,它们既等效又等同于说

are both valid and both equivalent because the pointer type is defined as a type specifier and you can put your type specifier and storage specifeir in any order. Note that they are both equivalent and would be equivalent to saying

static int *j;
static int *k;

int static *j;
int static *k;

这篇关于可以在声明中的何处放置存储类说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆