아래 코드는 32 바이트 정렬 메모리를 할당하는 사용자 지정 할당자를 정의합니다.32 바이트 정렬 메모리를 할당하는 할당 자
Visual Studio 2010에서 코드를 컴파일했습니다. 코드가 릴리스 모드에서 올바르게 컴파일됩니다. 디버그 모드에서 이해할 수없는 컴파일 오류가 발생합니다 (아래 참조). 코드에 어떤 문제가 있습니까? 디버그 모드에서
#include <memory>
#include <vector>
inline void* aligned_malloc(size_t n)
{
if(n == 0) return nullptr;
char* p = (char*)malloc(n + 32);
if(p == nullptr) return nullptr;
size_t offset = 32 - (size_t)p % 32;
p += offset;
*(p - 1) = (char)offset;
return p;
}
inline void aligned_free(void* p)
{
if(p == nullptr) return;
char offset = *((char*)p - 1);
free((char*)p - offset);
}
template<class T>
class AlignedAllocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
T* address(T& x) const { return &x; }
const T* address(const T& x) const { return &x; }
size_t max_size() const { return (size_t)(-1)/sizeof(T); }
T* allocate(size_t n, const T* = nullptr)
{
T* p = (T*)aligned_malloc(n * sizeof(T));
if(!p) throw std::bad_alloc();
return p;
}
void deallocate(pointer p, size_type) { aligned_free(p); }
void construct(T* p, const T& x) { new(p) T(x); }
void destroy(T* p) { p->~T(); }
template<class U> struct rebind { typedef AlignedAllocator<U> other; };
};
std::vector<int, AlignedAllocator<int>> v;
컴파일 오류 : 다른 것들 사이
c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(441): error C2440: 'initializing' : cannot convert from 'AlignedAllocator<T>' to 'AlignedAllocator<T>'
with
[
T=int
]
and
[
T=std::_Container_proxy
]
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(437) : while compiling class template member function 'std::_Vector_val<_Ty,_Alloc>::_Vector_val(_Alloc)'
with
[
_Ty=int,
_Alloc=AlignedAllocator<int>
]
c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(481) : see reference to class template instantiation 'std::_Vector_val<_Ty,_Alloc>' being compiled
with
[
_Ty=int,
_Alloc=AlignedAllocator<int>
]
c:\users\rade\desktop 5\higg boson repository\tmp\tmp.cpp(52) : see reference to class template instantiation 'std::vector<_Ty,_Ax>' being compiled
with
[
_Ty=int,
_Ax=AlignedAllocator<int>
]
고마워요. 문제가 해결되었습니다. – user763305