473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

<auto_ptr> inside <map> problem in VC 7!??

Hi, all!

I didn't find yet solution for this problem! Somebody knows where is a
catch?
Looks like "operator =" or copy constructor not implemented in one of
internal templates....

Thanks in advance

class CMyBase;
typedef auto_ptr<CMyBase> MyBasePtr;
typedef map<int, MyBasePtr> MAP_BASE;

class CMyBase
{
public:
CMyBase()
{ }
virtual ~CMyBase()
{ }
virtual MyBasePtr Clone(void) const
{ return MyBasePtr(new CMyBase()); }
virtual MyBasePtr Test()
{
MAP_BASE _map2;
_map2[1] = m_map.begin()->second->Clone();// Compiler error occured:
C2558: class 'std::auto_ptr<_Ty>' : no

//copy constructor available or copy constructor is declared 'explicit'
}
private:
MAP_BASE m_map;
};
Nov 16 '05 #1
3 3475
std::auto_ptr can not be used with most containers. One alternative is
boost::shared_ptr.

Dan

"Evgeny" <ev*****@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi, all!

I didn't find yet solution for this problem! Somebody knows where is a
catch?
Looks like "operator =" or copy constructor not implemented in one of
internal templates....

Thanks in advance

class CMyBase;
typedef auto_ptr<CMyBase> MyBasePtr;
typedef map<int, MyBasePtr> MAP_BASE;

class CMyBase
{
public:
CMyBase()
{ }
virtual ~CMyBase()
{ }
virtual MyBasePtr Clone(void) const
{ return MyBasePtr(new CMyBase()); }
virtual MyBasePtr Test()
{
MAP_BASE _map2;
_map2[1] = m_map.begin()->second->Clone();// Compiler error occured:
C2558: class 'std::auto_ptr<_Ty>' : no

//copy constructor available or copy constructor is declared 'explicit'
}
private:
MAP_BASE m_map;
};

Nov 16 '05 #2
Evgeny <ev*****@yahoo.com> wrote:
class CMyBase;
typedef auto_ptr<CMyBase> MyBasePtr;
typedef map<int, MyBasePtr> MAP_BASE;


you MAY NOT use std::auto_ptr inside STL containers! It's violation of
language rules and MSVC is right generating compilation error. That's
because *for auto_ptr, copies are NOT equivalent*. Here what Herb Sutter
says about it at
http://www.gotw.ca/publications/usin...ffectively.htm :
citation begin It is never safe to put auto_ptrs into standard containers. Some people
will tell you that their compiler and library compiles this fine, and
others will tell you that they've seen exactly this example recommended
in the documentation of a certain popular compiler; don't listen to
them.
The problem is that auto_ptr does not quite meet the requirements of a
type you can put into containers, because copies of auto_ptrs are not
equivalent. For one thing, there's nothing that says a vector can't just
decide to up and make an "extra" internal copy of some object it
contains. For another, when you call generic functions that will copy
elements, like sort() does, the functions have to be able to assume that
copies are going to be equivalent. At least one popular sort internally
takes a copy of a "pivot" element, and if you try to make it work on
auto_ptrs it will merrily take a copy of the pivot auto_ptr object
(thereby taking ownership and putting it in a temporary auto_ptr on the
side), do the rest of its work on the sequence (including taking further
copies of the now-non-owning auto_ptr that was picked as a pivot value),
and when the sort is over the pivot is destroyed and you have a problem:
At least one auto_ptr in the sequence (the one that was the pivot value)
no longer owns the pointer it once held, and in fact the pointer it held
has already been deleted! citation end


If you want to use smart pointers inside containers, use
boost::shared_ptr from http://www.boost.org/libs/smart_ptr/index.htm
Kind regards
B.
Nov 16 '05 #3
Thanks a lot!

"Bronek Kozicki" <Br**@rubikon.pl> wrote in message
news:28***************************@nf2.news-service.com...
Evgeny <ev*****@yahoo.com> wrote:
class CMyBase;
typedef auto_ptr<CMyBase> MyBasePtr;
typedef map<int, MyBasePtr> MAP_BASE;


you MAY NOT use std::auto_ptr inside STL containers! It's violation of
language rules and MSVC is right generating compilation error. That's
because *for auto_ptr, copies are NOT equivalent*. Here what Herb Sutter
says about it at
http://www.gotw.ca/publications/usin...ffectively.htm :
> citation begin It is never safe to put auto_ptrs into standard containers. Some people
will tell you that their compiler and library compiles this fine, and
others will tell you that they've seen exactly this example recommended
in the documentation of a certain popular compiler; don't listen to
them.
The problem is that auto_ptr does not quite meet the requirements of a
type you can put into containers, because copies of auto_ptrs are not
equivalent. For one thing, there's nothing that says a vector can't just
decide to up and make an "extra" internal copy of some object it
contains. For another, when you call generic functions that will copy
elements, like sort() does, the functions have to be able to assume that
copies are going to be equivalent. At least one popular sort internally
takes a copy of a "pivot" element, and if you try to make it work on
auto_ptrs it will merrily take a copy of the pivot auto_ptr object
(thereby taking ownership and putting it in a temporary auto_ptr on the
side), do the rest of its work on the sequence (including taking further
copies of the now-non-owning auto_ptr that was picked as a pivot value),
and when the sort is over the pivot is destroyed and you have a problem:
At least one auto_ptr in the sequence (the one that was the pivot value)
no longer owns the pointer it once held, and in fact the pointer it held
has already been deleted!> citation end


If you want to use smart pointers inside containers, use
boost::shared_ptr from http://www.boost.org/libs/smart_ptr/index.htm
Kind regards
B.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Evgeny | last post by:
Hi, all! I try to convert my existing code written in VC6 to VC7 and have some problem with stl auto pointer template. class CColumn; #include <memory> typedef auto_ptr<CMyBase> CMyBasePtr; ...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
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...
0
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...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.