2012-07-14 86 views
1

我一直在尝试使用basic_string和STL容器的自定义SecureAllocator,但我的运气很不好。C++自定义分配器和STL容器

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()) 
      ~~~~~~~~~^~~~~~~~~~~~~~~~ 

Envirnoment

的Mac OSX狮子

苹果铛版本3.1(标签/苹果/铛 - 318.0.61)(基于LLVM 3.1svn)

目标:x86_64的-apple-darwin11.4.0

线程模型:posix

+0

显示你分配吧。 – ForEveR 2012-07-14 15:47:06

回答

3

您必须为您的分配器类型实施比较运算符,告诉它们是否“等价”,以便它们可以互换(或不可)。

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

返回true只有当来自每个分配的存储可以通过其它被释放。 operator==应该是自反的,对称的和可传递的,并且不应该通过异常退出。

a1 != a2

一样!(a1 == a2)

+0

非常感谢,我一直盯着这几个小时。 – Corvusoft 2012-07-14 16:00:43