3312. polymorphic_allocator::allocate_object and new_object should be [[nodiscard]]

Section: 20.4.3 [mem.poly.allocator.class] Status: Dup Submitter: United States Opened: 2019-11-04 Last modified: 2019-11-16 12:41:07 UTC

Priority: Not Prioritized

View all other issues in [mem.poly.allocator.class].

View all issues with Dup status.


Addresses US 217

Add [[nodiscard]] in front of the return type for allocate_object and new_object in class declaration and in member-function description for polymorphic_allocator template.

[Daniel comments]

This issue is related to LWG 3304.

[2019-11 Status to Duplicate during Tuesday morning issue processing in Belfast.]

Duplicate of 3304.

Proposed resolution:

This wording is relative to N4835.

  1. Modify 20.4.3 [mem.poly.allocator.class] p2, class template polymorphic_allocator synopsis, as indicated:

    // [mem.poly.allocator.mem], member functions
    [[nodiscard]] Tp* allocate(size_t n);
    void deallocate(Tp* p, size_t n);
    void* allocate_bytes(size_t nbytes, size_t alignment = alignof(max_align_t));
    void deallocate_bytes(void* p, size_t nbytes, size_t alignment = alignof(max_align_t));
    template<class T> [[nodiscard]] T* allocate_object(size_t n = 1);
    template<class T> void deallocate_object(T* p, size_t n = 1);
    template<class T, class... CtorArgs> [[nodiscard]] T* new_object(CtorArgs&&... ctor_args);
    template<class T> void delete_object(T* p);
  2. Modify [mem.poly.allocator.mem] as indicated:

    template<class T>
      [[nodiscard]] T* allocate_object(size_t n = 1);

    -8- Effects: Allocates memory suitable for holding an array of n objects of type T, as follows:

    1. (8.1) — if SIZE_MAX / sizeof(T) < n, throws length_error,

    2. (8.2) — otherwise equivalent to:

      return static_cast<T*>(allocate_bytes(n*sizeof(T), alignof(T)));


    template<class T, class CtorArgs...>
      [[nodiscard]] T* new_object(CtorArgs&&... ctor_args);

    -11- Effects: Allocates and constructs an object of type T, as follows. Equivalent to:

    T* p = allocate_object<T>();
    try {
      construct(p, std::forward<CtorArgs>(ctor_args)...);
    } catch (...) {
    return p;