1

What does the term rebindable family types in C++ mean?

My query stems form the fact that allocators in c++ are supposed to be so-called rebindable family types.

And std::allocator has such a constructor:

template <typename U>
allocator(const allocator<U>& other);

alongwith

template<typename _Tp1>
        struct rebind
        { typedef new_allocator<_Tp1> other; };

Are there any custom examples to help understand that what is the need for this rebind to be there in first place within the std::allocator? What is the benefit it brings?

Test
  • 564
  • 3
  • 12
  • [Here are several answers](https://stackoverflow.com/questions/14148756/what-does-template-rebind-do) Is there something in particular you would like to know? – lakeweb Jan 12 '22 at 20:39
  • Where did you get this term? It's the first time I hear it. – Evg Jan 12 '22 at 20:59
  • @lakeweb - The link you shared doesn't answer my query. I have also updated my question to get a clear picture – Test Jan 13 '22 at 02:56
  • @Evg - I have added bit more context to the question. – Test Jan 13 '22 at 02:57
  • @Test: I'm not sure what is missing from those answers. I couldn't explain it better than that first answer. What do you not understand? – Nicol Bolas Jan 13 '22 at 04:28
  • 2
    The classic example is that `std::list` usually gets instantiated with `std::allocator` - an allocator that can allocate instances of `T`. But that the list really needs to allocate is something like `struct Node {T data; Node* left; Node* right};` So it needs a way to obtain an allocator for `Node` from an allocator for `T` it's given. That's where `rebind` comes in. – Igor Tandetnik Jan 13 '22 at 04:36
  • @IgorTandetnik - Could you elaborate a bit more? How will rebind help in this case? – Test Jan 13 '22 at 05:10
  • 2
    Something like `template std::list::list(const Alloc& alloc) {using NodeAlloc = typename Alloc::template rebind::other; NodeAlloc node_alloc(alloc); /* use node_alloc to allocate nodes */}` – Igor Tandetnik Jan 13 '22 at 05:16
  • A closely related [answer](https://stackoverflow.com/a/70630632) I wrote a few days ago. – Evg Jan 13 '22 at 06:18

0 Answers0