요약
rebind pattern은 제공하는 쪽에서는 클래스의 동작만을 정의하고 실제 type은 사용하는 쪽에서 결정할 수 있도록 하는 것이다.
 
 왜?
인터넷에서 검색한 몇가지 예를 보자

debuglab
 흐음.. 글쎄요. 뭐 제가 할당자에 대해서 자세히 공부한 것도 아니고 해서 정확하다고는 말씀드리지 못하겠지만요. 그냥 생각을 해보면, 실제 list 에 어떤 자료 T를 담는다고 해서 순수하게 T만 들어갈 수는 없잖아요. 그래서 T를 포함하고 앞 뒤 포인터를 가지는 다른 객체(리스트니까 당연하겠죠)로 리바인딩 해줘야 하고, 이를 위해서 할당자에 rebind가 존재하는 거겠죠. 그런데 여기다 T형 배열을 선언해 버리면, 생성자가 문제가 되어버립니다. 즉, 배열은 기본 생성자만 가능한데 보통 할당자에서 선언된 리바인딩 되어질 객체들은 사용자 정의 생성자를 사용하기 때문에 컴파일시 에러가 나는게 아닐까 합니다만.. (일부로 컴파일시 에러가 나게 하는 것일수도 있죠...)
 
즉 특정 type에서 할당하는 메모리의 type은 해당 type과 일치하지 않을 수도 있다. 그래서 다른 type의 메모리를 할당 할 수 있도록 type을 rebind시켜주는 것이 필요하게 되었다.
 
STL Tech
allocator에서 rebind를 선언내야 하는 이유
 stl의 container에서 vector를 제외한 list<>등은 해당 type이 아닌 별도의 type으로 allocate한 메모리를 필요로 한다.
rebind형의로 선언하고 그 type을 composite하고 있다.  allocator를 사용하는 client에서는 이 composite된 alloc_type을 사용하게 됨으로 client에서 지정한 type을 composite한 type의 memory로 allocate된다. template의 마술. :)
 
Gnarly New C++ Language Features
 
list가 int형으로 생성되는 경우 Node_allocator 를 생각해 생각해 보자.
 
typedef typename Allocator::rebind<listnode>::other  Node_allocator;
Allocator를 풀어 써 보면
typedef allocator<int>::rebind<listnode>::other Node_allocator ;
other를 풀어 써 보면
typedef allocator<int>::rebind<listnode>::allocator<listnode> Node_allocator ;
가 된다.
 
결국 Node_allocator의 type은 마지막에 있는 allocator<listnode>가 된다.
 
이제는 Node_allocator를 가지고 listnode형의 allocator를 메모리를 allocate하는 것이 아니라 int형의 메모리를 allocate하고 싶은 경우는 어떻게 해야 할까?
Node_allocator의 타입 선언을 조금 변경해 주면 된다.
typedef typename Allocator::rebind<T>::other  Node_allocator;
다시 풀어써 보면
typedef allocator<int>::rebind<int>::other Node_allocator ;
typedef allocator<int>::rebind<int>::allocator<int> Node_allocator ;
결국 int형의 allocator가 똑같은 int형의 allocator로 rebind된다.
 
C++: Understand rebind 
int type의로 initiate된 client클래스에서 int*의 메모리를 allocate하는 메모리 할당자를 선언하고 있다.
alloc_type을 풀어써 보자
typedef typename _Alloc::template rebind< U>::other alloc_type;
_Alloc을 풀어쓰면
typedef _Alloc_type<int>::template rebind<U>::other alloc_type ;
rebind를 풀어쓰면
typedef _Alloc_type<int>::rebind<int*>::other alloc_type ;
other를 풀어쓰면
typedef _Alloc_type<int>::rebind<int*>::_Alloc_type<int*> alloc_type ;
이렇게 해서 client클래스에서 alloc_type을 사용하게 되면 int* type의 _Alloc_type을 사용하게 된다.
 
그렇다면 왜 이렇게 사용하지 않는가?
즉 allocate하고 싶은 type(int*)를 직접 지정해서 initiate하면 되지 않는가?
대체 rebind를 사용하면 뭐가 더 좋아 지는가? 아래 예를 한번 보자.
client에서 지정한 _Alloc type으로 alloc_type이 결정됨으로 client_base에 정의된 동작은 수정하지 않고  client에서 지정한 type으로 allocate이 이루어 지게 된다.(Policy Clone)
 
참 어렵고도 재미있네요.
 
정리
list<T> class가 존재한다고 하고 실제 데이터를 포함할 memory외에 node에 위한 추가적인 memory가 필요한다.
구조체를 선언해 보면 아래와 같은 형태를 가질 것이다.
template<class T>
struct node {
 struct node next_ ;
 T* data_ ;
} ;
즉 실제 T type의 list를 필요로 하지만 memory에 allocate할때는 T type을 포함하는 node type을 allocate해야 한다는 것이다.
이때 rebind를 사용한다.
애초에 rebind의 형태가 이렇게 복잡하게 된 이유는 template에 대한 typedef를 지원하지 않기 때문이다.
하지만 member template 은 지원하므로 대부분 member template struct(rebind struct)를 이용해서 구현하게 된다. 그래서 rebind structure안에 other라는 type을 define하는 이유이다.
 
참조

+ Recent posts