2013-04-20 1 views
3

의 모음 인 Matrix 클래스가 있습니다.-std = C++ 0x를 사용할 때 사용자 정의 할당자를 사용하여 컴파일 문제가 발생했습니다.

행 :

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); 
    }; 
}; 
내가 -std=c++0x없이이를 컴파일 할 때이 오류없이 컴파일

#include "types.h" 

int main(int argc, char **argv) 
{ 
    Matrix<int> AA (100, 100); 
} 

: 16,

나는 코드를 테스트하기 위해 간단한 프로그램을 사용합니다.

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> >(); 

은 무엇 이것에 대한 이유가 될 수있다 : -std=c++0x을 사용하는 경우 그러나, 나는 다음과 같은 오류가? 가능한 해결 방법/해결 방법. gcc version 4.7.2clang version 3.1을 사용하고 있습니다.

(. 긴 코드에 대한 유감)은 운영자 ==를 가질> aligned_allocator <을 원한다처럼

답변

6

오류 메시지에 실제로 힌트가 들어 있습니다. 나는 약간 여기를 공식화했습니다 즉

error: invalid operands [of type aligned_allocator<int> ] to binary expression […] __x._M_get_Tp_allocator() == this->_M_get_Tp_allocator()

, 당신의 할당 유형은 operator ==를 제공해야합니다.

이것은 할당 자 요구 사항 (§17.6.3.5, 표 28)의 일부입니다. 이것은 항상 그랬다. 하지만 C++ 11 할당자가 무 상태이므로 operator ==은 항상 true을 반환하므로 표준 라이브러리 컨테이너에서는 연산자를 호출하지 않았을 것입니다. 이는 -std=++0x없이 코드가 컴파일되는 이유를 설명합니다.

2

이 보이는().