내 C++ 11 프로그램에서 활발하게 작성 및 삭제 된 일부 객체에 shared_ptr<T>
을 사용합니다. 표준 할당자인 operator new
은 병목 현상이 발생하므로 직접 작성하여 한 번에 많은 메모리를 할당 한 다음 요청시 make_shared
에 전달하십시오. Unfortunatelly, 이것은 내가 할당 쓰기 처음이며, GCC는 다음 코드를 컴파일 할 수없는 이유는 아무 생각이 : 여기allocate_shared/make_shared와 함께 사용자 정의 할당자를 사용할 수 없습니다.
#include <memory>
class MyAlloc {
public:
typedef char* pointer;
typedef const char* const_pointer;
typedef char value_type;
char* allocate(size_t len) {
return new char[len];
}
void deallocate(char *ptr) {
delete[] ptr;
}
} my_alloc;
int main() {
std::allocator_traits<MyAlloc>();
// MyAlloc is a correct allocator, since allocator_traits can be instantiated
// If I comment the following line of code, compilation is successful
std::allocate_shared<int>(my_alloc, 0);
return 0;
}
내가 아주 간단한 스텁 할당 및 allocate_shared
하나의 호출을합니다. GCC 오류는 다음과 같습니다.
In file included from c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\ext\alloc_traits.h:36:0,
from c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_construct.h:61,
from c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\memory:64,
from a.cpp:1:
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h: In substitution of 'template<class _Alloc> template<class _Tp> using rebind_traits = std::allocator_traits<typename std::__alloctr_rebind<_Alloc, _Tp>::__type> [with _Tp = std::_Sp_counted_ptr_inplace<int, MyAlloc, (__gnu_cxx::_Lock_policy)2u>; _Alloc = MyAlloc]':
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr_base.h:517:33: required from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = int; _Alloc = MyAlloc; _Args = {int}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr_base.h:986:35: required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = MyAlloc; _Args = {int}; _Tp = int; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr.h:316:64: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = MyAlloc; _Args = {int}; _Tp = int]'
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\shared_ptr.h:598:39: required from 'std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = int; _Alloc = MyAlloc; _Args = {int}]'
a.cpp:19:40: required from here
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h:204:66: error: invalid use of incomplete type 'struct std::__alloctr_rebind<MyAlloc, std::_Sp_counted_ptr_inplace<int, MyAlloc, (__gnu_cxx::_Lock_policy)2u>, false>'
using rebind_traits = allocator_traits<rebind_alloc<_Tp>>;
^
c:\soft\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\alloc_traits.h:65:12: error: declaration of 'struct std::__alloctr_rebind<MyAlloc, std::_Sp_counted_ptr_inplace<int, MyAlloc, (__gnu_cxx::_Lock_policy)2u>, false>'
struct __alloctr_rebind;
^
왜 이런 일이 발생합니까? 할당자를 올바르게 작성하여 allocate_shared
과 작동하도록하려면 어떻게해야합니까? 할당 자 (allocator)가 지원해야하는 다른 연산자와 유형 특성이 있음을 알고 있지만 GCC가 나에게 원하는 것에 대한 힌트는 볼 수 없습니다. 또한
그것은 void
shared_ptr<T>::some_weird_stuff
또는이보다 더 낫다처럼라도 (shared_ptr
와 함께) 특정 할당을 위해 value_type
char
등을 사용 OK인가?
'rebind' 회원 템플릿 (및 다른 것들)을 제공해야합니다. [allocator.requirements] 또는 [cppreference : Allocator requirements] (http://en.cppreference.com/w/cpp/concept/Allocator) – dyp