473,461 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

copy constructor problem with STL allocator

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
1 2778
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
26
by: Peter Olcott | last post by:
// // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? // #include <stdio.h> #include <stdlib.h> ...
3
by: Old Wolf | last post by:
My compiler's <string> header has this constructor (after preprocessing): template <class charT, class traits , class Allocator > class basic_string { template <class InputIterator>...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
5
by: jgscott | last post by:
I've been trawling around for an answer to this question and thought I'd try here. I have a class Graph, which has a std::list<Nodeas a class member. Node it itself a class that makes extensive...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
1
by: mersinli | last post by:
Hi, I have got seven error and I dont find why compiler find error.. //commission class definition #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.