By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

copy constructor problem with STL allocator

P: n/a
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>
>::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.

Dec 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
xq********@gmail.com wrote:
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>
>>::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

Dec 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.