简体   繁体   中英

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.

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 Lion

Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)

Target: x86_64-apple-darwin11.4.0

Thread model: 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

returns true only if storage allocated from each can be deallocated via the other. operator== shall be reflexive, symmetric, and transitive, and shall not exit via an exception.

and for a1 != a2

the same as !(a1 == a2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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