C++ list and private copy constructor

J

Jan

Hi There,
I want to insert an object with private copy constructor at the and of
a STL list.
Is there an easy way to do it like that:

list <x> myList; // x has private copy constructor
....

public void y::insert(x& obj)
{
myList.push_back(obj);
}
....

thanks,
Jan
 
N

Neelesh Bodas

Jan said:
Hi There,
I want to insert an object with private copy constructor at the and of
a STL list.
Is there an easy way to do it like that:

list <x> myList; // x has private copy constructor
...

public void y::insert(x& obj)
{
myList.push_back(obj);
}

Quoting the std 23.1p3
"The type of objects stored in these components must meet the
requirements of CopyConstructible types (20.1.3), and the additional
requirements of Assignable types."

In other words, the objects stored in the containers must be copyable
as well as destructable. Hence, this is not possible with the
private-copy constructor or private destructor.
 
Y

yuvalif

You can search for the topic: "How to insert immutable object into the
stl map"
 
R

Rolf Magnus

Jan said:
Hi There,
I want to insert an object with private copy constructor at the and of
a STL list.

That's not possible. C++ containers require the stored type to be copyable.
 
B

Bob Hairgrove

That's not possible. C++ containers require the stored type to be copyable.

Does the C++ standard allow making std::list<x> a friend of x?

In that case, it might have a chance of working (I don't mean to say
"it will work", though...)
 
R

Rolf Magnus

Bob said:
Does the C++ standard allow making std::list<x> a friend of x?

In that case, it might have a chance of working (I don't mean to say
"it will work", though...)

Hmm, I think that should work. However, in this case, the copy constructor
must be able to correctly copy the object, and the object must expected to
be copied by something else than itself. But in this case there is no good
reason to make the copy constructor private in the first place.
 
N

Neelesh Bodas

Bob said:
Does the C++ standard allow making std::list<x> a friend of x?

In that case, it might have a chance of working (I don't mean to say
"it will work", though...)

Appearantly making list<X> as a friend of X doesnot work. I was unable
to find a clear explanation for the same in the standard. But standard
clearly says that the objects must be copy-constructable and
assignable.So, it is very well possible for an implementation to have
push_back call some other (non-member) function to do the copy of the
object. In that case the copy-construction won't be possible.
 
P

Pete Becker

Neelesh said:
Appearantly making list<X> as a friend of X doesnot work. I was unable
to find a clear explanation for the same in the standard.

There's no need for a clear explanation, because, as you observe,
[The] standard
clearly says that the objects must be copy-constructable and
assignable.

So the explanation is simple: containers don't have to work right with
types that aren't copy constructible and assignable, even if someone
thinks they might.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top