逗号中的逗号运算符

此代码在Visual Studio 2019上编译并运行良好(输出5)

#include <iostream>



int main(void) {

    alignas(2,3,9,8) int x=5;

    std::cout << x;


    return 0;
}

in C comma expressions are explicitly disallowed in alignas. This compiled fine here. I changed the order and placed 9 or 3 at the end (which is disallowed in alignas) and the program failed to compiling indicating the expression in the brackets is really being parsed as the comma operator. Is there a different meaning here or is this just VS allowing the programmer to get away with something they shouldn't.

评论
  • 抱抱熊
    抱抱熊 回复

    The standard actually says in C11 6.6 emphasis mine:

    常量表达式不得包含赋值,递增,递减,函数调用或逗号运算符,除非它们包含在未求值的子表达式中。 115)

    然后在脚注中:

    115)通常不评估sizeof或_Alignof运算符的操作数。