Title
allocator_traits::max_size() default behavior is incorrect
Status
c++17
Section
[allocator.requirements][allocator.traits.members]
Submitter
Howard Hinnant

Created on 2015-01-17.00:00:00 last changed 89 months ago

Messages

Date: 2015-05-07.23:01:40

Proposed resolution:

This wording is relative to N4296.

  1. Change [allocator.requirements], Table 28 — "Allocator requirements", as indicated:

    Table 28 — Allocator requirements
    Expression Return type Assertion/note
    pre-/post-condition
    Default
    a.max_size() X::size_type the largest value that can
    meaningfully be passed to
    X::allocate()
    numeric_limits<size_type>::max()/sizeof(value_type)
  2. Change [allocator.traits.members]/p7 as indicated:

    static size_type max_size(const Alloc& a) noexcept;
    

    Returns: a.max_size() if that expression is well-formed; otherwise, numeric_limits<size_type>::max()/sizeof(value_type).

Date: 2015-05-07.23:01:40

[ 2015-05, Lenexa ]

Marshall: Is this the right solution?
PJP: I think it's gilding the lily.
STL: I think this is right, and it doesn't interact with the incomplete container stuff because it's in a member function.
Marshall: Objections to this?
STL: Spaces around binary operators.
Hwrd: It's completely wrong without spaces.
Marshall: All in favor of Ready?
Lots.

Date: 2015-01-17.00:00:00

Table 28 — "Allocator requirements" says that default behavior for a.max_size() is numeric_limits<size_type>::max(). And this is consistent with the matching statement for allocator_traits in [allocator.traits.members]/p7:

static size_type max_size(const Alloc& a) noexcept;

Returns: a.max_size() if that expression is well-formed; otherwise, numeric_limits<size_type>::max().

However, when allocating memory, an allocator must allocate n*sizeof(value_type) bytes, for example:

value_type*
allocate(std::size_t n)
{
  return static_cast<value_type*>(::operator new (n * sizeof(value_type)));
}

When n == numeric_limits<size_type>::max(), n * sizeof(value_type) is guaranteed to overflow except when sizeof(value_type) == 1.

A more useful default would be numeric_limits<size_type>::max() / sizeof(value_type).

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2015-10-27 16:52:45adminsetstatus: ready -> wp
2015-05-07 23:01:40adminsetmessages: + msg7389
2015-05-07 23:01:40adminsetstatus: new -> ready
2015-01-19 21:36:56adminsetmessages: + msg7225
2015-01-17 00:00:00admincreate