私は現在、メモリデバッガを作成しています。そのためには、非トラッキングアロケータを使用するためにstlコンテナオブジェクトが必要です。std :: string with custom allocator
私は、文字列が私の全体のコードベース全体にちりばめ::はstdてきたので、私は私の人跡未踏のアロケータを使用するためにそれをtypedefさ:
String str { "Some string" };
String copy = str;
:私はこれを行うにしようとすると、今
typedef std::basic_string<char, std::char_traits<char>, UntrackedAllocator<char>> String;
を
/usr/local/include/c++/7.1.0/ext/alloc_traits.h:95:67: error: no matching function for call to 'UntrackedAllocator<char>::UntrackedAllocator(UntrackedAllocator<char>)' { return _Base_type::select_on_container_copy_construction(__a); }
これは私の人跡未踏アロケータがどのように見えるかです:私はこのエラーを取得する
#pragma once
#define NOMINMAX
#undef max
template <typename T>
class UntrackedAllocator {
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public:
template<typename U>
struct rebind {
typedef UntrackedAllocator<U> other;
};
public:
inline explicit UntrackedAllocator() {}
inline ~UntrackedAllocator() {}
inline explicit UntrackedAllocator(UntrackedAllocator const&) {}
template<typename U>
inline explicit UntrackedAllocator(UntrackedAllocator<U> const&) {}
// address
inline pointer address(reference r) {
return &r;
}
inline const_pointer address(const_reference r) {
return &r;
}
// memory allocation
inline pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
T *ptr = (T*)malloc(cnt * sizeof(T));
return ptr;
}
inline void deallocate(pointer p, size_type cnt) {
free(p);
}
// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max()/sizeof(T);
}
// construction/destruction
inline void construct(pointer p, const T& t) {
new(p) T(t);
}
inline void destroy(pointer p) {
p->~T();
}
inline bool operator==(UntrackedAllocator const& a) { return this == &a; }
inline bool operator!=(UntrackedAllocator const& a) { return !operator==(a); }
};
これは私の初めてのカスタムアロケータでの作業で、何が起こっているのか分かりません。 str1 = str2のうちの1つがカスタムアロケータを使用する場合、str1 = str2を実行できないということは信じられないほど気にならない。
関係演算子が間違っています。アロケータは、他のアロケートの割当を解除できるときはいつでも等しいと見なすべきです。 –
http://stackoverflow.com/help/mcve –
エラーメッセージは、コピーコンストラクタをどこかで使用していることを示しています。添え字演算子が値渡しで返されない限り、投稿したコードはコンストラクタのコピーを使用しません。とにかく、コピーコンストラクタを明示的にすることはめったに役に立ちません。コピーコンストラクタから 'explicit'を削除してください。 –