AUTO_PTR issue.

Started by
9 comments, last by edb6377 11 years, 9 months ago
I am having a heck of time converting this one piece of an old VS6 program to VS2005

Error 161 error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit' C:\Program Files\Microsoft Visual Studio 8\VC\include\utility 37
Error 230 error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit' C:\Program Files\Microsoft Visual Studio 8\VC\include\vector 1125
Error 256 error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or copy constructor is declared 'explicit' C:\Program Files\Microsoft Visual Studio 8\VC\include\utility 37

Header: (Added // ERROR IS HERE where its breaking)
[source lang="cpp"]// utility standard header
#pragma once
#ifndef _UTILITY_
#define _UTILITY_
#ifndef RC_INVOKED
#include <iosfwd>

#ifdef _MSC_VER
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#endif /* _MSC_VER */

_STD_BEGIN
// TEMPLATE FUNCTION swap (from <algorithm>)
template<class _Ty> inline
void swap(_Ty& _Left, _Ty& _Right)
{ // exchange values stored at _Left and _Right
_Ty _Tmp = _Left;
_Left = _Right, _Right = _Tmp;
}

// TEMPLATE STRUCT pair
template<class _Ty1,
class _Ty2> struct pair
{ // store a pair of values
typedef pair<_Ty1, _Ty2> _Myt;
typedef _Ty1 first_type;
typedef _Ty2 second_type;

pair()
: first(_Ty1()), second(_Ty2())
{ // construct from defaults
}

pair(const _Ty1& _Val1, const _Ty2& _Val2)
: first(_Val1), second(_Val2)
{ // construct from specified values
// ERROR IS HERE
}

template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}

void swap(_Myt& _Right)
{ // exchange contents with _Right
std::swap(first, _Right.first);
std::swap(second, _Right.second);
}

_Ty1 first; // the first stored value
_Ty2 second; // the second stored value
};

// pair TEMPLATE FUNCTIONS
template<class _Ty1,
class _Ty2> inline
bool operator==(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test for pair equality
return (_Left.first == _Right.first && _Left.second == _Right.second);
}

template<class _Ty1,
class _Ty2> inline
bool operator!=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test for pair inequality
return (!(_Left == _Right));
}

template<class _Ty1,
class _Ty2> inline
bool operator<(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left < _Right for pairs
return (_Left.first < _Right.first ||
!(_Right.first < _Left.first) && _Left.second < _Right.second);
}

template<class _Ty1,
class _Ty2> inline
bool operator>(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left > _Right for pairs
return (_Right < _Left);
}

template<class _Ty1,
class _Ty2> inline
bool operator<=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left <= _Right for pairs
return (!(_Right < _Left));
}

template<class _Ty1,
class _Ty2> inline
bool operator>=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left >= _Right for pairs
return (!(_Left < _Right));
}

template<class _Ty1,
class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{ // return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}

template<class _Ty1,
class _Ty2> inline
void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
{ // swap _Left and _Right pairs
_Left.swap(_Right);
}

// TEMPLATE OPERATORS
namespace rel_ops
{ // nested namespace to hide relational operators from std
template<class _Ty> inline
bool operator!=(const _Ty& _Left, const _Ty& _Right)
{ // test for inequality, in terms of equality
return (!(_Left == _Right));
}

template<class _Ty> inline
bool operator>(const _Ty& _Left, const _Ty& _Right)
{ // test if _Left > _Right, in terms of operator<
return (_Right < _Left);
}

template<class _Ty> inline
bool operator<=(const _Ty& _Left, const _Ty& _Right)
{ // test if _Left <= _Right, in terms of operator<
return (!(_Right < _Left));
}

template<class _Ty> inline
bool operator>=(const _Ty& _Left, const _Ty& _Right)
{ // test if _Left >= _Right, in terms of operator<
return (!(_Left < _Right));
}
}
_STD_END

#ifdef _MSC_VER
#pragma warning(pop)
#pragma pack(pop)
#endif /* _MSC_VER */

#endif /* RC_INVOKED */
#endif /* _UTILITY_ */

/*
* Copyright © 1992-2005 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
*/

/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright © 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
V4.05:0009 */
[/source]
Advertisement
map<DWORD, auto_ptr< PacketInterpreter > > interpreters;

Is one of the calls its breaking on.
While researching this question I found a bunch of links saying "don't use auto_ptr in map". It looks like Microsoft agrees with them and actually made it a comipiler error by marking auto_ptr's constructor explicit. One solution would be to use a different kind of smart pointer that does support copy-semantics.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

You could use std::shared_ptr instead?
Yeah i will have to look into boost i guess. Worked in VC6 and I find it funny it interesting they didnt consider that with the new includes. I might have to check out BOOST
As of C++11 std::auto_ptr is deprecated.
The closest analogue might be std::unique_ptr:
http://herbsutter.com/gotw/_103/ -- read this for an explanation of the current situation (std::auto_ptr being deprecated) and what are the available choices
http://home.roadrunn...ique_ptr03.html
http://www.informit....plus&seqNum=403

Note, however, that there are differences (including that of unique_ptr having move semantics -- at the same time, "copying" std::auto_ptr was always tricky: http://en.wikipedia....o_ptr#Semantics):
http://stackoverflow...o-stdunique-ptr

In a broader context another choice might be boost::scoped_ptr, however, it cannot be stored in std. lib. containers:
http://www.boost.org.../scoped_ptr.htm

Comparisons:
http://stackoverflow...-stdunique-ptrt
http://stackoverflow...oost-equivalent
http://shatteredterm...smart-pointers/

While in C++03 standard library containers stored objects had to be CopyConstructible and Assignable, in C++11 the requirements got relaxed to MoveConstructible and MoveAssignable for many operations, allowing to have containers of unique_ptr:
http://stackoverflow...nts-for-stdlist

However, the compiler support for this is still a WIP:
http://stackoverflow...que-ptr-and-map
http://stackoverflow...econstructibles

Another route to take is to use Boost Pointer Container Library:
http://www.boost.org.../ptr_container/
In particular, you might be interested in the following:
- ptr_map - http://www.boost.org...oc/ptr_map.html
- ptr_map_adapter - http://www.boost.org...ap_adapter.html
- ptr_unordered_map - http://www.boost.org...ordered_map.hpp
Wow thank you for the ton of information. I will start soaking that up now. Its good to know I need to start developing a plan for these as I move my code up to current standards and implementation.

Thank you much!
So it appears I am going to have to rewrite a whole lot of my code from the vc6 version. It makes use of auto_ptr in this manner all over the place. Even when i got it to compile it started having issues with resetting the value and deleting the value. It wasn't returning what it should in a number of ways.. Once i get it all figured out I will add to this post so others know what I had to do. I tried to use a few different methods to get around the issue by rewriting the memory.h version but that obviously didn't go well. Its hard to troubleshoot until you can actually return the program to working order. Time to start looking through all that great information smile.png

I couldn't find a simple method or drop in version. It appears its going to be a big rewrite.
If you have access to a modern compiler with unique_ptr then you could basically replace each usage of auto_ptr with unique_ptr and make some simple alterations to the code based on error messages and it ought to work (is there a good reason for why are you porting to VS 2005?).

If you're changing auto_ptr to unique_ptr then you have to add a call to std::move in some cases. For example, if a and b are auto_ptrs, then this code is valid:
a=b

But, the unique_ptr version must be written like this:
a=move(b)

You can just use the error messages to find all of the places where you need a call to move.
VC6, although it was released several months after the first C++ standard was published, came with a non-conforming version of the C++ standard library (there was a lawsuit going on at the time of release that prevent Microsoft from bundling correct code). One of the areas of greatest nonconformity was the std::auto_ptr API. It changed quite a bit between the start of the lawsuit and the release of the C++ standard.

It's no surprise that you will have to make considerable modifications to your software if it's based on pre-standard code released more than 15 years ago. That's several generation in internet time.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement