Is it possible to store unique objects in an STL container?
Suppose an object of class C is unique:
class C
{
public:
C() {}
~C() {}
private:
C(const C&);
operator=(const C&);
};
Another class manages and operates on collections of these objects:
class User
{
public:
void addC(const C& c)
{
m_lstCs.push_back(c);//error!
}
protected:
std::list<C> m_lstCs;
};
This leads to an error, because std::list<C>::push_back() calls C's
copy constructor. Is a list of pointers the only solution, or is there
a better way?
--
Claudio Jolowicz
Department of Computing
180 Queen's Gate
South Kensington campus
Imperial College
LONDON SW7 2AZ http://www.doc.ic.ac.uk/~cj603 22 2681
"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk... Is it possible to store unique objects in an STL container?
Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor. Is a list of pointers the only solution, or is there a better way?
One of the requirements of containers is that objects
stored in them must be both 'copyable' and 'assignable'.
So yes, a pointer would be the only way.
-Mike
"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk... Is it possible to store unique objects in an STL container?
Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor. Is a list of pointers the only solution, or is there a better way?
One of the requirements of containers is that objects
stored in them must be both 'copyable' and 'assignable'.
So yes, a pointer would be the only way.
-Mike
On Fri, 9 Apr 2004 03:34:09 +0100, Claudio Jolowicz <cj***@doc.ic.ac.uk>
wrote: Is it possible to store unique objects in an STL container?
Is "unique" a technical term I haven't encountered yet? There are
"Singletons", but by definition you wouldn't have more than one, so I can't
for the life of me imagine why you'd /want/ to be able to store them in a
container.
Another possible meaning of "unique" would be as in "Is it possible to have
an STL container in which all objects have unique values". Sure, that's
what std::set is, a collection of uniquely-valued objects. Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
What is it about Cs that make them "unique"? They can't be copy constructed
or assigned, but that wouldn't stop you from having a bunch of them...
whether they have equivalent values or not. Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor.
That's a basic requirement for storing any object in an STL container: that
it can be copied.
Is a list of pointers the only solution, or is there a better way?
Whatever it is you're trying to do, a collection of pointers (smart or
otherwise) would be a work-around for collecting non-copyable objects in
STL containers. I can't tell you if there's a "better way", because I can't
understand what you're trying to do.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Fri, 9 Apr 2004 03:34:09 +0100, Claudio Jolowicz <cj***@doc.ic.ac.uk>
wrote: Is it possible to store unique objects in an STL container?
Is "unique" a technical term I haven't encountered yet? There are
"Singletons", but by definition you wouldn't have more than one, so I can't
for the life of me imagine why you'd /want/ to be able to store them in a
container.
Another possible meaning of "unique" would be as in "Is it possible to have
an STL container in which all objects have unique values". Sure, that's
what std::set is, a collection of uniquely-valued objects. Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
What is it about Cs that make them "unique"? They can't be copy constructed
or assigned, but that wouldn't stop you from having a bunch of them...
whether they have equivalent values or not. Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor.
That's a basic requirement for storing any object in an STL container: that
it can be copied.
Is a list of pointers the only solution, or is there a better way?
Whatever it is you're trying to do, a collection of pointers (smart or
otherwise) would be a work-around for collecting non-copyable objects in
STL containers. I can't tell you if there's a "better way", because I can't
understand what you're trying to do.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Fri, 9 Apr 2004, Leor Zolman wrote: On Fri, 9 Apr 2004 03:34:09 +0100, Claudio Jolowicz <cj***@doc.ic.ac.uk> wrote:
Is it possible to store unique objects in an STL container? Is "unique" a technical term I haven't encountered yet? There are "Singletons", but by definition you wouldn't have more than one, so I can't for the life of me imagine why you'd /want/ to be able to store them in a container.
Another possible meaning of "unique" would be as in "Is it possible to have an STL container in which all objects have unique values". Sure, that's what std::set is, a collection of uniquely-valued objects.
Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
What is it about Cs that make them "unique"? They can't be copy constructed or assigned, but that wouldn't stop you from having a bunch of them... whether they have equivalent values or not.
To clarify, "unique" was used in the sense of having copy operations
private, as in Stroustrup, 3rd ed. p.249 (§ 10.4.6.3). Theoretically, I
agree there could be another, equivalent instance. But you might not be
able to create such an equivalent instance, because you don't know the
exact type, or you don't have access to private data, etc. Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor.
That's a basic requirement for storing any object in an STL container: that it can be copied.
Is a list of pointers the only solution, or is there a better way?
Whatever it is you're trying to do, a collection of pointers (smart or otherwise) would be a work-around for collecting non-copyable objects in STL containers. I can't tell you if there's a "better way", because I can't understand what you're trying to do. -leor
Some of the objects represent items in a trading environment (my post
yesterday), each item having a unique ID. Another class defines an agent
running on a thread. It might be more reasonable to allow copy
construction and define an equivalence relation using the item/thread
IDs. Copy assignment is a problem since associations between items are
represented as references. But as far as I know (please correct me),
copy assignment is not required for elements of a std::list. -- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
--
Claudio Jolowicz
Department of Computing
180 Queen's Gate
South Kensington campus
Imperial College
LONDON SW7 2AZ http://www.doc.ic.ac.uk/~cj603
On Fri, 9 Apr 2004, Leor Zolman wrote: On Fri, 9 Apr 2004 03:34:09 +0100, Claudio Jolowicz <cj***@doc.ic.ac.uk> wrote:
Is it possible to store unique objects in an STL container? Is "unique" a technical term I haven't encountered yet? There are "Singletons", but by definition you wouldn't have more than one, so I can't for the life of me imagine why you'd /want/ to be able to store them in a container.
Another possible meaning of "unique" would be as in "Is it possible to have an STL container in which all objects have unique values". Sure, that's what std::set is, a collection of uniquely-valued objects.
Suppose an object of class C is unique:
class C { public: C() {} ~C() {} private: C(const C&); operator=(const C&); };
What is it about Cs that make them "unique"? They can't be copy constructed or assigned, but that wouldn't stop you from having a bunch of them... whether they have equivalent values or not.
To clarify, "unique" was used in the sense of having copy operations
private, as in Stroustrup, 3rd ed. p.249 (§ 10.4.6.3). Theoretically, I
agree there could be another, equivalent instance. But you might not be
able to create such an equivalent instance, because you don't know the
exact type, or you don't have access to private data, etc. Another class manages and operates on collections of these objects:
class User { public: void addC(const C& c) { m_lstCs.push_back(c);//error! } protected: std::list<C> m_lstCs; };
This leads to an error, because std::list<C>::push_back() calls C's copy constructor.
That's a basic requirement for storing any object in an STL container: that it can be copied.
Is a list of pointers the only solution, or is there a better way?
Whatever it is you're trying to do, a collection of pointers (smart or otherwise) would be a work-around for collecting non-copyable objects in STL containers. I can't tell you if there's a "better way", because I can't understand what you're trying to do. -leor
Some of the objects represent items in a trading environment (my post
yesterday), each item having a unique ID. Another class defines an agent
running on a thread. It might be more reasonable to allow copy
construction and define an equivalence relation using the item/thread
IDs. Copy assignment is a problem since associations between items are
represented as references. But as far as I know (please correct me),
copy assignment is not required for elements of a std::list. -- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
--
Claudio Jolowicz
Department of Computing
180 Queen's Gate
South Kensington campus
Imperial College
LONDON SW7 2AZ http://www.doc.ic.ac.uk/~cj603
"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk... Is it possible to store unique objects in an STL container?
[where "unique" is intended to mean "non-copyable"]
Non-copyable object instances shall usaually be either
termporaries stored on the stack, or globals, or heap-based
objects.
So yes, you want to use a container of pointers -- but
preferably "smart pointers" to help avoid memory leaks.
My advice would be:
std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr.
(note that shared_ptr is expected to be included in the next
revision of the C++ standard).
hth,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk... Is it possible to store unique objects in an STL container?
[where "unique" is intended to mean "non-copyable"]
Non-copyable object instances shall usaually be either
termporaries stored on the stack, or globals, or heap-based
objects.
So yes, you want to use a container of pointers -- but
preferably "smart pointers" to help avoid memory leaks.
My advice would be:
std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr.
(note that shared_ptr is expected to be included in the next
revision of the C++ standard).
hth,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Claudio Jolowicz wrote: [...] as far as I know (please correct me), copy assignment is not required for elements of a std::list.
23[lib.containers]/3: 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.
--
Regards,
Buster.
Claudio Jolowicz wrote: [...] as far as I know (please correct me), copy assignment is not required for elements of a std::list.
23[lib.containers]/3: 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.
--
Regards,
Buster.
Ivan Vecerina wrote: So yes, you want to use a container of pointers -- but preferably "smart pointers" to help avoid memory leaks.
My advice would be: std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
X-posted to a.c.l.l.c-c++ due to newsfeed problems.
Brian Rodenborn
Ivan Vecerina wrote: So yes, you want to use a container of pointers -- but preferably "smart pointers" to help avoid memory leaks.
My advice would be: std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
X-posted to a.c.l.l.c-c++ due to newsfeed problems.
Brian Rodenborn
Default User wrote in news:40***************@boeing.com.invalid: Ivan Vecerina wrote:
So yes, you want to use a container of pointers -- but preferably "smart pointers" to help avoid memory leaks.
My advice would be: std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
No (IIUC), the revesion was an update, i.e. it incorparates all
the DR's that were effectivly bug's in the prior Standard (C++98).
In effect the Standard hasn't changed, Its just now you can
buy a more accurate version.
I went here: http://std.dkuug.dk/jtc1/sc22/wg21/
and eventually found: http://std.dkuug.dk/jtc1/sc22/wg21/d...al_report.html
There's about 10 boost libraries on the list (II<Count>C)
Its only a Technical *Report* so its still uncertain wether these libs
will end up in the next standard.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
Default User wrote in news:40***************@boeing.com.invalid: Ivan Vecerina wrote:
So yes, you want to use a container of pointers -- but preferably "smart pointers" to help avoid memory leaks.
My advice would be: std::list< boost::shared_ptr<C> > m_lstCs;
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
No (IIUC), the revesion was an update, i.e. it incorparates all
the DR's that were effectivly bug's in the prior Standard (C++98).
In effect the Standard hasn't changed, Its just now you can
buy a more accurate version.
I went here: http://std.dkuug.dk/jtc1/sc22/wg21/
and eventually found: http://std.dkuug.dk/jtc1/sc22/wg21/d...al_report.html
There's about 10 boost libraries on the list (II<Count>C)
Its only a Technical *Report* so its still uncertain wether these libs
will end up in the next standard.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid... Ivan Vecerina wrote: See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
Not yet, as the latest C++ standard still dates back to 1998.
However, library extensions are actively being worked on,
and the boost.org repository of libraries
Some extensions are already "approved" for future inclusion,
and even supported by a fewcompilers in their preliminary form (the
recommendation for now being to keep these extensions in std::tr1).
See: http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1540.pdf
Boost users will find a few familiar classes in this round of
proposed extensions, and probably in the next ones...
This is yet another reason to get familiar with these
peer-reviewed libraries.
Regards,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
On Fri, 9 Apr 2004 07:06:17 +0100 in comp.lang.c++, Claudio Jolowicz
<cj***@doc.ic.ac.uk> wrote, To clarify, "unique" was used in the sense of having copy operations private, as in Stroustrup, 3rd ed. p.249 (§ 10.4.6.3).
I guess "singleton" is now the standard name for that,
probably from _Design Patterns_ by Gamma et al.
"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid... Ivan Vecerina wrote: See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
Not yet, as the latest C++ standard still dates back to 1998.
However, library extensions are actively being worked on,
and the boost.org repository of libraries
Some extensions are already "approved" for future inclusion,
and even supported by a fewcompilers in their preliminary form (the
recommendation for now being to keep these extensions in std::tr1).
See: http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1540.pdf
Boost users will find a few familiar classes in this round of
proposed extensions, and probably in the next ones...
This is yet another reason to get familiar with these
peer-reviewed libraries.
Regards,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
On Fri, 9 Apr 2004 07:06:17 +0100 in comp.lang.c++, Claudio Jolowicz
<cj***@doc.ic.ac.uk> wrote, To clarify, "unique" was used in the sense of having copy operations private, as in Stroustrup, 3rd ed. p.249 (§ 10.4.6.3).
I guess "singleton" is now the standard name for that,
probably from _Design Patterns_ by Gamma et al.
Rob Williscroft wrote: Default User wrote in news:40***************@boeing.com.invalid:
Was any part of Boost added to the standard in the recent revision?
No (IIUC), the revesion was an update, i.e. it incorparates all the DR's that were effectivly bug's in the prior Standard (C++98).
So no new stuff this time around.
I went here: http://std.dkuug.dk/jtc1/sc22/wg21/
I always seem to get lost maneuvering around there :) http://std.dkuug.dk/jtc1/sc22/wg21/d...al_report.html
There's about 10 boost libraries on the list (II<Count>C)
Thanks!
Its only a Technical *Report* so its still uncertain wether these libs will end up in the next standard.
Yeah, it will be interesting to see what makes it into the Standard.
Brian Rodenborn
Rob Williscroft wrote: Default User wrote in news:40***************@boeing.com.invalid:
Was any part of Boost added to the standard in the recent revision?
No (IIUC), the revesion was an update, i.e. it incorparates all the DR's that were effectivly bug's in the prior Standard (C++98).
So no new stuff this time around.
I went here: http://std.dkuug.dk/jtc1/sc22/wg21/
I always seem to get lost maneuvering around there :) http://std.dkuug.dk/jtc1/sc22/wg21/d...al_report.html
There's about 10 boost libraries on the list (II<Count>C)
Thanks!
Its only a Technical *Report* so its still uncertain wether these libs will end up in the next standard.
Yeah, it will be interesting to see what makes it into the Standard.
Brian Rodenborn
Ivan Vecerina wrote: "Default User" <fi********@boeing.com.invalid> wrote in message news:40***************@boeing.com.invalid...
Ivan Vecerina wrote:
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
Not yet, as the latest C++ standard still dates back to 1998.
ISO/IEC 14882:2003, you mean? It dates back to 1998 in the
sense that it replaces ISO/IEC 14882:1998 by incorporating
a number of corrections. No new features though.
-- James
Ivan Vecerina wrote: "Default User" <fi********@boeing.com.invalid> wrote in message news:40***************@boeing.com.invalid...
Ivan Vecerina wrote:
See boost.org for a (thread-safe) implementation of shared_ptr. (note that shared_ptr is expected to be included in the next revision of the C++ standard).
Was any part of Boost added to the standard in the recent revision?
Not yet, as the latest C++ standard still dates back to 1998.
ISO/IEC 14882:2003, you mean? It dates back to 1998 in the
sense that it replaces ISO/IEC 14882:1998 by incorporating
a number of corrections. No new features though.
-- James This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael Winter |
last post by:
Is the Standard Template Library capable of storing complete objects rather
than pointers. For example, would either of the vectors below (a, c)
function correctly (note the member in C).
class...
|
by: Claudio Jolowicz |
last post by:
Is it possible to store unique objects in an STL container?
Suppose an object of class C is unique:
class C
{
public:
C() {}
~C() {}
private:
|
by: Matthias Kaeppler |
last post by:
Hi,
in my program, I have to sort containers of objects which can be 2000
items big in some cases. Since STL containers are based around copying
and since I need to sort these containers quite...
|
by: Cory Nelson |
last post by:
Does anyone know how std::set prevents duplicates using only std::less?
I've tried looking through a couple of the STL implementations and
their code is pretty unreadable (to allow for different...
|
by: George Exarchakos |
last post by:
Hi everyone,
I'd like your help...
Can we have a std::list<BASEwhere BASE be the base class of a class
hierarchy? I want to add to this list objects that are inherited from
BASE class but not...
|
by: r.z. |
last post by:
I logged construtor and destructor calls for one of my classes and I
discovered that the constructor was called only once while the destructor
was called 3 times for a single object.
I have a...
|
by: tonytech08 |
last post by:
How valuable is it that class objects behave like built-in types? I
appears that the whole "constructor doesn't return a value because
they are called by the compiler" thing is to enable...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |