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

C++ list and private copy constructor

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

Dec 5 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

Jan wrote:
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.

Dec 5 '05 #2

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

Dec 5 '05 #3

P: n/a
You can search for the topic: "How to insert immutable object into the
stl map"

Dec 5 '05 #4

P: n/a
On Mon, 05 Dec 2005 16:38:20 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
Jan wrote:
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.


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...)

--
Bob Hairgrove
No**********@Home.com
Dec 5 '05 #5

P: n/a
Bob Hairgrove wrote:
On Mon, 05 Dec 2005 16:38:20 +0100, Rolf Magnus <ra******@t-online.de>
wrote:
Jan wrote:
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.


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.

Dec 5 '05 #6

P: n/a

Bob Hairgrove wrote:
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.

Dec 6 '05 #7

P: n/a
Neelesh Bodas wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Dec 6 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.