Ben Crowhurst
Ben Crowhurst

Reputation: 8481

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

Upvotes: 1

Views: 432

Answers (1)

Bo Persson
Bo Persson

Reputation: 92211

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)

Upvotes: 3

Related Questions