变量的const和volatile的顺序 [英] Order of const and volatile for a variable
问题描述
以下代码段可在 gcc版本4.7.2(Debian 4.7.2-5)
中编译并运行:
The following piece of code compiles and runs with gcc version 4.7.2 (Debian 4.7.2-5)
:
#include <stdio.h>
int main()
{
const volatile x = 3;
volatile const y = 4;
return 0;
}
我是否应该认为const和volatile的顺序无关紧要?我在这里尝试阅读: encpp ref ,它没有什么都没说(或我想念它?)
Should I assume that the order of const and volatile is irrelevant? I tried reading up here : encpp ref and it doesn't say anything about the order(or I'm missing it?)
推荐答案
是的,顺序无关紧要.在C ++中,相关规范在7.1p1, decl-specifier 和 decl-specifier-seq 中,它们基本上解释了相关关键字的序列以及7.1.6,其中列出了 const
和 volatile
作为这两个关键字.请注意,虽然奇怪,但出于可读性考虑,我还是强烈建议对它们进行验证:
Yes, the order is irrelevant. In C++, the relevant specification is in 7.1p1, decl-specifier and decl-specifier-seq, which basically explain that there is a sequence of relevant keywords, and 7.1.6, which lists const
and volatile
as two these keywords. Note that the production is weird enough that these are valid as well, though in the interest of readability I would strongly recommend against them:
const int volatile a = 1;
volatile int const b = 2;
const int volatile typedef vcint; // defines vcint to be an alias for const volatile int
这篇关于变量的const和volatile的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!