2016-05-28 17 views
1

私は現在、メモリデバッガを作成しています。そのためには、非トラッキングアロケータを使用するために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を実行できないということは信じられないほど気にならない。

+2

関係演算子が間違っています。アロケータは、他のアロケートの割当を解除できるときはいつでも等しいと見なすべきです。 –

+0

http://stackoverflow.com/help/mcve –

+2

エラーメッセージは、コピーコンストラクタをどこかで使用していることを示しています。添え字演算子が値渡しで返されない限り、投稿したコードはコンストラクタのコピーを使用しません。とにかく、コピーコンストラクタを明示的にすることはめったに役に立ちません。コピーコンストラクタから 'explicit'を削除してください。 –

答えて

1

問題は、コピーの宣言がexplicitであることです。

inline UntrackedAllocator(UntrackedAllocator const&) {} 

は、コンパイルの問題を解決し、すべてがうまく動作します::これはconst std::basic_string &を受け入れstd::basic_stringの代入演算子が必要で行われるため

int main() { 
    String str { "13" }; 
    String copy = str; 
    const char* cstr = str.c_str(); 
    int out = atoi(cstr); 
} 

UntrackedAllocatorコピーc'torを変更しますアロケータの暗黙のコピー構築。

関連する問題