C++自定义分配器和STL容器

C++ custom allocators and STL containers

本文关键字:STL 容器 分配器 自定义 C++      更新时间:2023-10-16

我一直在尝试使用带有basic_string和STL容器的自定义SecureAllocater,但运气很差。

typedef std::basic_string< char, std::char_traits< char >, SecureAllocator< char > > SecureString;
SecureString value = "hello, world!";
vector< SecureString > collection;
collection.push_back( value );

In file included from /Users/bcrowhurst/source/utility/string_impl.cpp:31:
In file included from /Users/bcrowhurst/build/../source/utility/string_impl.h:31:
/usr/bin/../lib/c++/v1/string:2162:19: error: invalid operands to binary expression ('allocator_type' (aka 'SecureAllocator<char>') and 'allocator_type')
        if (__alloc() != __str.__alloc())
            ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~

环境

Mac OSX Lion

Apple clang 3.1版(标签/Apple/crang-318.0.61)(基于LLVM 3.1svn)

目标:x86_64-apple-darwin11.4.0

线程模型:posix

您必须为分配器类型实现比较运算符,判断它们是否"等价",以便可以互换(或不互换)使用。

比较两个分配器a1 == a2的要求是

仅当从每个分配的存储可以通过另一个释放时,返回true。operator==应是自反的、对称的和传递的,并且不应通过异常退出。

对于a1 != a2

!(a1 == a2) 相同