473,700 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<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<CMyBas e> 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 3503
std::auto_ptr can not be used with most containers. One alternative is
boost::shared_p tr.

Dan

"Evgeny" <ev*****@yahoo. com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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<CMyBas e> 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<CMyBas e> 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_p tr from http://www.boost.org/libs/smart_ptr/index.htm
Kind regards
B.
Nov 16 '05 #3
Thanks a lot!

"Bronek Kozicki" <Br**@rubikon.p l> wrote in message
news:28******** *************** ****@nf2.news-service.com...
Evgeny <ev*****@yahoo. com> wrote:
class CMyBase;
typedef auto_ptr<CMyBas e> 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_p tr 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
4329
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; class CMyBase {
10
2969
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 constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
0
8649
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9081
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8975
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4408
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3097
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2032
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.