2012-07-14 99 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