Skip to content

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

Open
@xmh0511

Description

@xmh0511
Contributor

[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])).

Activity

frederick-vs-ja

frederick-vs-ja commented on Jul 24, 2023

@frederick-vs-ja
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

xmh0511 commented on Jul 24, 2023

@xmh0511
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @xmh0511@frederick-vs-ja

        Issue actions

          [expr.new] p20 Accurate the example for the allocation call in array new expression · Issue #5930 · cplusplus/draft