问题
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;
}
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?)
回答1:
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
来源:https://stackoverflow.com/questions/24325108/order-of-const-and-volatile-for-a-variable