vendredi 6 septembre 2019

Why does the alignas specifier throw an error on Clang?

According to the standard, the new expression's grammar allows the alignas specifier along with other attribute specifiers to be supplied. I have seen other code using this but GCC ignores the attribute specifier, even if it is within the max alignment, and Clang throws an error.

  • Is the alignas specifier actually valid within a new expression?
  • If it is valid, why does Clang reject my code and why does GCC ignore my attribute no matter what alignment I request?

I am not trying to solve a specific problem, I am just trying to understand the standard.

    int* example = new alignas(16) int[2];
    // alignof(std::max_align_t) = 16
    // sizeof(int) = 4, sizeof(example) = 8

GCC:

prog.cc: In function 'int main()':
prog.cc:7:41: warning: attribute ignored [-Wattributes]
    7 |     int* example = new alignas(16) int[2];
      |                                         ^
prog.cc:7:41: note: an attribute that appertains to a type-specifier is ignored

Clang:

 prog.cc:7:24: error: an attribute list cannot appear here:
               int* example = new alignas(16) int[2];

Aucun commentaire:

Enregistrer un commentaire