template <typename T>
class stl_allocator
{
public:
using value_type = T;
using pointer = std::shared_ptr<T[]>;
stl_allocator() = default;
template <typename U>
stl_allocator(stl_allocator<U> const&) {}
pointer allocate(std::size_t n)
{
return pointer{::new T[n]};
}
void deallocate(pointer, std::size_t) {}
template <typename... Args>
void construct(pointer p, Args&&... args)
{
::new(p.get()) T{std::forward<Args>(args)...};
}
void destroy(pointer) {}
template <typename U>
bool operator ==(stl_allocator<U> const&) { return true; }
template <typename U>
bool operator !=(stl_allocator<U> const&) { return false; }
};
namespace std
{
template <typename T>
ptrdiff_t operator -(shared_ptr<T[]> const &lhs, shared_ptr<T[]> const &rhs)
{
return lhs.get() - rhs.get();
}
}
どうにも私の環境(MSVC2015)ではコンパイルできません
deallocate の呼び出し時に 生のポインタを std::shared_ptr<T[]> に変換しようとして
失敗しているようなのですが...
カスタムアロケータの知識に乏しいもので、難解なテンプレートのエラーに悩まされております
必要なメンバ関数も足りていないかもしれません
更に、std::hash や std::default_delete などの特殊化は規格でも許容されていたはずですが、
operator - の追加は大丈夫なのか気になります
拙いコードと文章で申し訳ありませんがよろしくお願いします
[anchor= goto=http://www.slideshare.net/Cryolite/allocator11final]http://www.slideshare.net/Cryolite/allocator11final[/anchor]