Skip to content

Instantly share code, notes, and snippets.

@BlurryLight
Created June 26, 2023 15:53
Show Gist options
  • Save BlurryLight/393c07f8c5965ccd31014b2f36e9da57 to your computer and use it in GitHub Desktop.
Save BlurryLight/393c07f8c5965ccd31014b2f36e9da57 to your computer and use it in GitHub Desktop.
fixed (pmr) vector by fixed-size allocator
template <class T, size_t N> struct limited_allocator : std::allocator<T> {
using value_type = typename std::allocator<T>::value_type;
using size_type = typename std::allocator<T>::size_type;
size_type max_size() const noexcept { return N; }
template <class Other> struct rebind {
typedef limited_allocator<Other, N> other;
};
// make msvc happy
template <class Other, size_t M> constexpr operator limited_allocator<Other, M>() const noexcept {
return limited_allocator<Other, M>();
}
};
template <class T, size_t N> struct limited_pmr_allocator : std::pmr::polymorphic_allocator<T> {
using value_type = typename std::pmr::polymorphic_allocator<T>::value_type;
using size_type = typename std::allocator_traits<std::pmr::polymorphic_allocator<T>>::size_type;
size_type max_size() const noexcept { return N; }
template <class Other> struct rebind {
typedef limited_pmr_allocator<Other, N> other;
};
template <class Other, size_t M> constexpr operator limited_pmr_allocator<Other, M>() const noexcept {
return limited_pmr_allocator<Other, M>{this->resource()};
}
};
template <class T, size_t N> using limited_vector = std::vector<T, limited_allocator<T, N>>;
template <class T, size_t N> using limited_pmr_vector = std::vector<T, limited_pmr_allocator<T, N>>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment