C++ list and private copy constructor

Discussion in 'C++' started by Jan, Dec 5, 2005.

  1. Jan

    Jan Guest

    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
     
    Jan, Dec 5, 2005
    #1
    1. Advertising

  2. 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.
     
    Neelesh Bodas, Dec 5, 2005
    #2
    1. Advertising

  3. Jan

    Guest

    You can search for the topic: "How to insert immutable object into the
    stl map"
     
    , Dec 5, 2005
    #3
  4. Jan

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 5, 2005
    #4
  5. On Mon, 05 Dec 2005 16:38:20 +0100, Rolf Magnus <>
    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
     
    Bob Hairgrove, Dec 5, 2005
    #5
  6. Jan

    Rolf Magnus Guest

    Bob Hairgrove wrote:

    > On Mon, 05 Dec 2005 16:38:20 +0100, Rolf Magnus <>
    > 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.
     
    Rolf Magnus, Dec 5, 2005
    #6
  7. 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.
     
    Neelesh Bodas, Dec 6, 2005
    #7
  8. Jan

    Pete Becker Guest

    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)
     
    Pete Becker, Dec 6, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    4,132
    Alf P. Steinbach
    Mar 3, 2006
  2. Fraser Ross
    Replies:
    7
    Views:
    553
    James Kanze
    Aug 7, 2007
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,248
  4. cinsk
    Replies:
    35
    Views:
    2,615
    James Kanze
    Oct 11, 2010
  5. David Jobet

    private copy constructor question

    David Jobet, Nov 29, 2010, in forum: C++
    Replies:
    9
    Views:
    974
    David Jobet
    Dec 2, 2010
Loading...

Share This Page