xqxu.pzhou@gmail.com wrote:
Quote:
I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is
used by Vector, it works well. But when it is used by List, the codes
have errors when compling. the error message is:
"no matching function for call to myAlloc<std::_List_node<int>
Quote:
>>::myAlloc(const myAlloc<int>&), candidates are: myAlloc<T>::myAlloc(const
>>::myAlloc<T>& ) [with T = std::_List_node<int>]".
Your allocator needs to conform to the allocator requirements as defined in
the standard [20.1.5]
Quote:
>
Then I added the following copy constructor to "myAlloc", now it works
well.
"template <typename U>
myAlloc(const myAlloc<U>&) {}"
Yup, this copy constructor is required. See Table 31 and 32.
Quote:
I don't understand such copy constructor very well. I don't know why we
should use such copy constructor. In my opinion, this copy constructor
doesn't do anything.
Doesn't matter whether it does something. It's a type shifting thing: the
list wants to allocate nodes of some internal type. You are passing an
allocator for a different type. The list needs to get an allocator for the
type it wants from the allocator you provided. Thus, the list uses this
copy constructor.
Quote:
In order to know the detail, I delete this copy constructor in
"myAlloc", and I also delete all parts that will call above copy
constructor in file "stl_list.h". The result is that the List works
well. it seems that the list does not need the above copy constructor
any more.
Well, we have no way to tell which kind of undefined behavior causes your
modifies code appear to work.
Quote:
So I'm wonder why we need that copy constructor, and what's the task of
that copy constructor.
See above.
Best
Kai-Uwe Bux