Order of const and volatile for a variable

北慕城南 提交于 2021-02-16 20:15:09

问题


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

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