The allocator is a template argument, if you decide to implement one for your particular use case it will be active only in those objects for which you explicitly opt into this allocator:
std::vector<T,SecureAllocator> v; // this uses the memset_s under the hood
std::vector<T> n; // this doesn't
Now, the allocator modifies the type of the object, which means that if you have functions that take std::vector<T>
as arguments you will not be able to pass a std::vector<T,SecureAllocator>
.
Alternatively, you could implement a polymorphic allocator in which the source of the memory can be controlled at runtime. That is supported in BSL (an implementation of the C++03 standard library available in github), in which case the vectors are of the same type even if they allocate from different sources:
bsl::vector<T> v(bslma::Default::allocator());
// bslma::Default::allocator() is new/delete
bsl::vector<T> n(secureAllocator());