我有一个Matrix
类,它是Row
s 的集合。数据类型定义如下:
排:
template <typename Index>
class Row {
public:
Row()
{
_index_vector = std::vector<Index, aligned_allocator<Index> > ();
}
Row& operator=( const Row& source )
{
//some copy logic here
return *this;
}
private:
std::vector<Index, aligned_allocator<Index> > _index_vector;
};
矩阵:
template <typename Index>
class Matrix {
public:
typedef std::vector<Row<Index> > Rep;
Matrix () : _m (0), _n (0)
{}
Matrix (size_t n, size_t m) :
_m (m),
_n (n)
{
_A = Rep (n);
}
private:
Rep _A;
size_t _m;
size_t _n;
};
Row
数据类型使用分配器,主要功能有:
template <class T>
class aligned_allocator
{
public:
//Other methods; members...
pointer allocate ( size_type size, const_pointer *hint = 0 ) {
pointer p;
posix_memalign((void**)&p, 16, size * sizeof (T));
return p;
};
void construct ( pointer p, const T& value ) {
*p=value;
};
void destroy ( pointer p ) {
p->~T();
};
void deallocate ( pointer p, size_type num ) {
free(p);
};
};
我使用这个简单的程序来测试代码:
#include "types.h"
int main(int argc, char **argv)
{
Matrix<int> AA (100, 100);
}
当我在没有 的情况下编译它时-std=c++0x
,它编译时没有任何错误。但是,当-std=c++0x
启用时,我收到以下错误:
error: invalid operands to binary expression ('_Tp_alloc_type'
(aka 'aligned_allocator<int>') and '_Tp_alloc_type')
if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
./types.h:26:17: note: in instantiation of member function 'std::vector<int, aligned_allocator<int> >::operator=' requested here
_index_vector = std::vector<Index, aligned_allocator<Index> > ();
这可能是什么原因?以及可能的修复/解决方法。我正在使用gcc version 4.7.2
和clang version 3.1
。
(对不起,冗长的代码。)