Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.new] p20 Accurate the example for the allocation call in array new expression #5930

Open
xmh0511 opened this issue Nov 4, 2022 · 2 comments

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Nov 4, 2022

[expr.new] p19 says

Overload resolution is performed on a function call created by assembling an argument list.
The first argument is the amount of space requested, and has type std​::​size_­t. If the type of the allocated object has new-extended alignment, the next argument is the type's alignment, and has type std​::​align_­val_­t. If the new-placement syntax is used, the initializer-clauses in its expression-list are the succeeding arguments. If no matching function is found then:

  • [...]
  • otherwise, an argument that is the type's alignment and has type std​::​align_­val_­t is added into the argument list immediately after the first argument;

Anyway, for the type refers to the type of the allocated object. [expr.new] p20 gives a misleading example of array object

new T[5] results in one of the following calls: 
operator new[](sizeof(T) * 5 + x)
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)))

operator new[](sizeof(T) * 5 + x, 2, f)
operator new[](sizeof(T) * 5 + x, std::align_val_t(alignof(T)), 2, f)

In such two cases, we specify the argument with the form std::align_val_t(alignof(T)), which is the alignment of the type of the element of the array, even though the alignment of the array type is equivalent to that of the type of the element according to [expr.alignof] p3. Note that the allocated object is an array.

In order to be consistent with what the rule exactly says, we should use std::align_val_t(alignof(T[5])).

@frederick-vs-ja
Copy link
Contributor

I don't think using alignof(T[5]) improves consistency.

In this example, 5 is a constant expression, so we happen to be able to write std::align_val_t(alignof(T[5])). However, if one uses n that is not a constant expression, the expositing expression needs to fall back to use alignof(T). So it's arguably more consistent to always mention the element type only.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Jul 24, 2023

I don't think using alignof(T[5]) improves consistency.

In this example, 5 is a constant expression, so we happen to be able to write std::align_val_t(alignof(T[5])). However, if one uses n that is not a constant expression, the expositing expression needs to fall back to use alignof(T). So it's arguably more consistent to always mention the element type only.

We should separate the rule into two cases: one for array objects and one for the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants