Skip to content

Consider updating Allocator Access to add rvalue reference overloads #156

Description

@cmazakas

Right now most of allocator access takes the allocator by lvalue reference.

I presume this is to support stateful allocators.

Instead, I purport that allocator access should also work with temporarily materialized allocators.

This is useful in the case where we're rebinding a stored allocator and wishing to do a quick one-off allocation in a single place.

The goal is to remove the need to declare a stack-local, for example.

See the relevant CE example here: https://godbolt.org/z/hqKTGb3ab

Obligatory inline copy:

#include <boost/core/allocator_access.hpp>
#include <memory>
#include <type_traits>

namespace boost
{

template<class A>
allocator_pointer_t<std::decay_t<A>>
allocator_allocate(A&& a, allocator_size_type_t<std::decay_t<A>> n)
{
  return allocator_allocate(a,n);
}

}

auto tester(std::allocator<int> a) {
  // feed the rebound allocator directly using the converting constructor
  //
  return boost::allocator_allocate(std::allocator<float>(a),16);

  // want to avoid the need to declare `oa` to use the rebound allocator type
  //
  // std::allocator<float> oa(a);
  // return boost::allocator_allocate(oa,16);
}

Activity

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

      Issue actions