简体   繁体   English

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

[英]C++ custom allocators and STL containers

I've been attempting to use a custom SecureAllocator with basic_string and the STL containers but I'm having very little luck. 我一直在尝试使用带有basic_string和STL容器的自定义SecureAllocator,但我的运气很少。

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 Envirnoment

Mac OSX Lion Mac OSX Lion

Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn) Apple clang 3.1版(标签/ Apple / clang-318.0.61)(基于LLVM 3.1svn)

Target: x86_64-apple-darwin11.4.0 目标:x86_64-apple-darwin11.4.0

Thread model: posix 线程模型:posix

You have to implement comparison operators for your allocator type, telling if they are 'equivalent' so they can be used interchangably (or not). 您必须为分配器类型实现比较运算符,告诉它们是否“等效”,以便它们可以互换(或不互换)。

The requirement for comparing two allocators a1 == a2 is 比较两个分配器a1 == a2

returns true only if storage allocated from each can be deallocated via the other. 仅当从每个分配的存储可以通过另一个分配时,才返回true。 operator== shall be reflexive, symmetric, and transitive, and shall not exit via an exception. operator==应具有自反性,对称性和传递性,不得通过异常退出。

and for a1 != a2 并为a1 != a2

the same as !(a1 == a2) !(a1 == a2)相同!(a1 == a2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM