-3

C2x, 6.5.3.4 The sizeof and _Alignof operators, Semantics, 2 (emphasis added):

If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

C2x, 6.7.6.2 Array declarators, Semantics, 5 (emphasis added):

Where a size expression is part of the operand of an _Alignof operator, that expression is not evaluated.

Consider this code:

int f(void)
{
    return _Alignof( int(*)[ f() ] );
}

Question: shall f() be called?

Per 6.5.3.4 the type of the operand is a variable length array type => the operand is evaluated.

Per 6.7.6.2 the size expression is part of the operand of an _Alignof operator => that expression is not evaluated.

Is there a contradiction in the standard?

If no, then does it mean that 6.7.6.2 have higher priority than 6.5.3.4?

pmor
  • 5,392
  • 4
  • 17
  • 36
  • 3
    Where, in the Standard, does it say that the 6.5.3.4 (paragraph 2) that you cite applies to `__Alignof`? In the Standard I just read (albeit C11), that paragraph opens with: *The sizeof operator yields the size (in bytes) of its operand, ...* You can't just go around chopping out parts of the Standard that don't suit your question. – Adrian Mole Feb 23 '22 at 16:08
  • 3
    Just one paragraph below (C6.5.3.4 -3, C11) it says: *The _Alignof operator yields the alignment requirement of its operand type. The operand is not evaluated and the result is an integer constant. When applied to an array type, the result is the alignment requirement of the element type.* That also makes sense as the alignment requirements of a type do not change if you create an array of it. – Gerhardh Feb 23 '22 at 16:11
  • And the two points made above apply equally in [this C2x Standard document](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf). – Adrian Mole Feb 23 '22 at 16:13
  • expression vs type. In your case it is a type (the type of a VLA), then 6.5.3.4 applies. A (weird) size expression would be `_Alignof(x+=3)`, 6.7.6.2 applies and `x+=3` will not be evaluated. Am I wrong? – Jean-Baptiste Yunès Feb 23 '22 at 16:16
  • @AdrianMole Thanks. I overlooked it. – pmor Feb 23 '22 at 18:29

1 Answers1

0

The standard was overlooked.

There is no contradiction.

To clarify:

C11, 6.5.3.4 The sizeof and _Alignof operators, Semantics, 3 (emphasis added):

The _Alignof operator yields the alignment requirement of its operand type. The operand is not evaluated and the result is an integer constant.

It means that in

_Alignof( int(*)[ f() ] )

the

int(*)[ f() ]

is not evaluated.

Hence, the f() is not called.

Extra: ICC incorrectly produces an error:

int f(void);
int s = _Alignof( int(*)[ f() ] );

$ icc -std=c11 -pedantic -Wall -Wextra -c
error: function call is not allowed in a constant expression

Here the function call is allowed because such funtion call is

contained within a subexpression that is not evaluated

pmor
  • 5,392
  • 4
  • 17
  • 36