Search code examples
c++c++11gccclang++icc

no conversion from "std::allocator" to "const allocator_type"


I am trying to implement an aligned allocator for use with MKL in C++11. I have:

template <typename T, size_t TALIGN = 16, size_t TBLOCK = 4>
class aligned_allocator : public std::allocator<T>
{
    typedef typename std::allocator<T>::pointer pointer;
    typedef typename std::allocator<T>::size_type size_type;
public:
    pointer allocate(size_type n, const void *hint = nullptr);
    void deallocate(pointer p, size_type n);
};

Elsewhere, I have:

template<typename T> using aligned_vector = std::vector<T, aligned_allocator<T>>;

and finally, I have this operator overload:

inline aligned_vector<double> operator+(aligned_vector<double> x, aligned_vector<double> y)
{
    aligned_vector<double> z(x.size());
    vdAdd(x.size(), x.data(), y.data(), z.data());
    return z;
}

This all compiles and works perfectly under both icc and clang, but with GCC 4.9, it won't compile unless I make x and y both const-references. Why does GCC require this when the others don't?


Solution

  • You're missing rebind:

    template <typename U> struct rebind { typedef aligned_allocator<U> other; };
    

    That said, you shouldn't inherit from std::allocator: Why not to inherit from std::allocator