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

Consider updating Allocator Access to add rvalue reference overloads #156

Open
cmazakas opened this issue Sep 12, 2023 · 0 comments
Open

Comments

@cmazakas
Copy link
Member

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);
}
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

1 participant