copy constructor problem with STL allocator

X

xqxu.pzhou

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 said:
::myAlloc(const myAlloc<int>&), candidates are: myAlloc<T>::myAlloc(const myAlloc<T>& ) [with T = std::_List_node<int>]".

Then I added the following copy constructor to "myAlloc", now it works
well.
"template <typename U>
myAlloc(const myAlloc<U>&) {}"

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.
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.
So I'm wonder why we need that copy constructor, and what's the task of
that copy constructor.
TIA.
 
K

Kai-Uwe Bux

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 said:
::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]
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.
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.
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top