我正在尝试实现一个与C++11中的MKL一起使用的对齐分配器。
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);
};在其他地方,我有:
template<typename T> using aligned_vector = std::vector<T, aligned_allocator<T>>;最后,我有一个操作符超载:
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;
}所有这些都在icc和clang下完美地编译和工作,但是使用GCC 4.9,它不会编译,除非我让x和y都是const引用。为什么GCC要求这个,而其他人却不这样做?
发布于 2014-08-11 20:11:38
你错过了rebind
template <typename U> struct rebind { typedef aligned_allocator<U> other; };也就是说,您不应该继承std::allocator:Why not to inherit from std::allocator
https://stackoverflow.com/questions/25251356
复制相似问题