What is the difference between static const int and static int const?

北战南征 提交于 2020-04-16 03:32:48

问题


In this answer the OP used:

static int const var = 5;

in the context of a Conditional Compilation Control.


Is there a difference between using static const int and static int const?

Like for example:

static const int var;

vs.

static int const var;

I don´t know the technique to imply the type in the middle between static and const. Is there a difference?


回答1:


The grammar for declaration specifiers is given in C 2018 6.7 1, and it shows that specifiers for storage class (such as static), type (such as short or double), qualifiers (such as const), functions (inline and _Noreturn), and alignment may appear in any order. Nothing in clause 6.7 gives any meaning to the order in which the specifiers appear, so we may presume any combination of specifiers has the same meaning regardless of order.

The only mention of “order” in this regard appears in 6.7.2 2, which says “… the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.” So you can write long static int const long for static const long long int, just as you can say “square red big house” instead of “big square red house”—there is no rule against it, but it will seem funny to people and may throw them off.

Note that the * that indicates a pointer, as well as ( and ) for either grouping or argument lists and [ and ] for subscripts are not declaration specifiers and may not be freely reordered with declaration specifiers. (They are in fact part of a declarator, which is a separate part of a declaration from the declaration-specifiers.)



来源:https://stackoverflow.com/questions/60438923/what-is-the-difference-between-static-const-int-and-static-int-const

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!