C++ 带有自定义分配器的std::string

C++ 带有自定义分配器的std::string,c++,stl,allocator,C++,Stl,Allocator,因此,我目前正在编写一个内存调试器,为此我需要stl容器对象使用一个未跟踪的分配器 我的整个代码库中都有std::string,所以我键入def以使用我的未跟踪分配器: typedef std::basic_string<char, std::char_traits<char>, UntrackedAllocator<char>> String; 我得到这个错误: /usr/local/include/c++/7.1.0/ext/alloc_traits.h

因此,我目前正在编写一个内存调试器,为此我需要stl容器对象使用一个未跟踪的分配器

我的整个代码库中都有std::string,所以我键入def以使用我的未跟踪分配器:

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); }
/usr/local/include/c++/7.1.0/ext/alloc_traits.h:95:67:错误:调用“UntrackedAllocator::UntrackedAllocator(UntrackedAllocator)(UntrackedAllocator){return_Base_type::select_on_container__copy_构造(u 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); }
};
#pragma一次
#定义NOMINMAX
#未定义最大值
模板
类未跟踪分配器{
公众:
类型定义T值_类型;
typedef值\u type*指针;
类型定义常量值\u类型*常量指针;
类型定义值\u类型和参考;
类型定义常量值\u类型和常量参考;
typedef std::size\u t size\u type;
typedef std::ptrdiff_t difference_type;
公众:
模板
结构重新绑定{
typedef未跟踪Allocator或其他;
};
公众:
内联显式未跟踪Allocator(){}
内联~UntrackedAllocator(){}
内联显式untrackedlocator(untrackedlocator常量&){}
模板
内联显式untrackedlocator(untrackedlocator常量&){}
//地址
内联指针地址(参考r){
返回&r;
}
内联常量指针地址(常量参考r){
返回&r;
}
//内存分配
内联指针分配(大小\类型cnt,
typename std::分配器::常量指针=0){
T*ptr=(T*)malloc(cnt*sizeof(T));
返回ptr;
}
内联无效释放(指针p,大小\类型cnt){
自由基(p);
}
//大小
内联大小\类型最大大小()常量{
返回std::numeric_limits::max()/sizeof(T);
}
//建造/销毁
内联void构造(指针p、常量T&T){
新(p)T(T);
}
内联无效销毁(指针p){
p->~T();
}
内联布尔运算符==(未跟踪的Allocator常量&a){返回this==&a;}
内联布尔运算符!=(未跟踪的Allocator常量&a){return!运算符==(a);}
};

这是我第一次使用自定义分配器,所以我不知道这是怎么回事。如果其中一个使用自定义分配器,我就不能执行str1=str2,这令人难以置信地恼火

问题在于将复制任务声明为
explicit

UntrackedAllocator
copy c'tor更改为:

inline UntrackedAllocator(UntrackedAllocator const&) {}
解决了编译问题,一切正常:

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

这是因为接受
const std::basic_字符串&
std::basic_字符串的赋值运算符需要分配器的隐式副本构造。

您的关系运算符错误。只要一个分配器可以解除分配另一个分配器的分配,分配器就应该比较相等。错误消息暗示您正在某处使用复制构造函数。您发布的代码不使用复制构造函数,除非您的下标运算符按值返回。无论如何,使复制构造函数显式很少有用。从复制构造函数中删除
显式
,一切都会好起来。@KerrekSB你是什么意思?
int main() {
  String str { "13" };
  String copy = str;
  const char* cstr = str.c_str();
  int out = atoi(cstr);
}