Search code examples
c++templatesc++11allocationstdstring

Base class should be explicitly initialized in the copy constructor


I've seen answers for this in other topics on Stack Overflow, but I'm not sure what to do in my case. I'm brand new to templates and this is the first template I've /ever/ created using examples found, again, from this website. Anyhow, here is the code below and the associated error:

warning: base class 'class std::allocator<char>' should be explicitly initialized in the copy constructor [-Wextra] SecureString(const SecureString &) throw() {}

    template<class T> class SecureString : public std::allocator<T> {
    public:
    template<class U> struct rebind { typedef SecureString<U> other; };
    SecureString() throw() {}
    SecureString(const SecureString &) throw() {}
    template <class U> SecureString(const SecureString<U>&) throw() {}
    void deallocate(T *p, size_t n) {
        #ifdef _WIN32
        SecureZeroMemory((void *)p, n);
        #elif __linux__
        std::fill_n((volatile char *)p, (n * sizeof(T)), 0);
        #endif
        std::allocator<T>::deallocate(p, n);
    }
};

There are more errors but that's the main one. I learn best from examples so any help would be greatly appreciated, thank you.


Solution

  • This is what I got from the warning message (copy-construct the base, too):

    SecureString(SecureString const& other) throw() : std::allocator<T>(other) {}
    

    Better leave it up do default if you don't plan to do anything useful there:

    SecureString(SecureString const&) throw() = default;
    

    And consider dumping the throw() exception specification.