#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
// g++ allocator_traits_test.cpp -std=c++11 -fPIC -Ofast -D_GLIBCXX_USE_CXX11_ABI=0 -o allocator_traits_test
template <class T>
struct custom_allocator {
typedef T value_type;
custom_allocator() noexcept {}
template <class U> custom_allocator (const custom_allocator<U>&) noexcept {}
T* allocate (std::size_t n) { return static_cast<T*>(::operator new(n*sizeof(T))); }
void deallocate (T* p, std::size_t n) { ::operator delete(p); }
};
typedef std::allocator_traits<custom_allocator<char>>::rebind_alloc<int> other_allcator;
template <class T, class U>
constexpr bool operator== (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return true;}
template <class T, class U>
constexpr bool operator!= (const custom_allocator<T>&, const custom_allocator<U>&) noexcept
{return false;}
int main () {
std::vector<int, other_allcator> foo = {10,20,30};
for (auto x: foo) std::cout << x << " ";
std::cout << '\n';
return 0;
}
c++ allocator_traits
最新推荐文章于 2023-12-29 08:30:00 发布