Which C99-compiler (Clang vs. GCC) is closer to standard on const structure fields?

后端 未结 2 761
野的像风
野的像风 2020-12-11 18:55

I have code like this:

$ cat test.c 
#include 
typedef struct
{
    const int x;
} SX;

static SX mksx(void)
{
    return (SX) { .x = 10 };
}
         


        
2条回答
  •  不思量自难忘°
    2020-12-11 19:31

    The C99 standard says in 6.5.16:2:

    An assignment operator shall have a modifiable lvalue as its left operand.

    and in 6.3.2.1:1:

    A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

    So GCC is right to warn.

    In addition, the clause 6.5.16:2 is in a “Constraints” section of the C99 standard, so a conforming compiler is required to emit a diagnostic for a program that breaks the clause. It is still undefined behavior: the compiler can still do what it wants after the diagnostic is emitted. But there has to be a message. In consequence, Clang is behaving in a non-conforming manner here.

提交回复
热议问题