Sign in to follow this  
edb6377

AUTO_PTR issue.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
While researching this question I found a bunch of links saying [url="http://stackoverflow.com/questions/8280828/is-it-safe-to-use-stdauto-ptr-in-stdmap"]"don't use auto_ptr in map"[/url]. 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.

Share this post


Link to post
Share on other sites
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 Edited by edb6377

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I couldn't find a simple method or drop in version. It appears its going to be a big rewrite. Edited by edb6377

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I will give another go from clean version to try and port it again.. I actually chose 2005 because i hate 2003 and it was the next iteration. A straight move to 2010 was daunting and i feel will give me more issues than i can handle at once. I figured if i can get it to 2005 correctly then i will move to 2008 and finally to 2010. That way I slowly break up the problem children.

To be honest these days everything I create is 2010 and will move as new versions are released but this particular project was an old one that needs to have many major updates. I.e. DX7 etc. So in order to do that I am trying to get my application in a managed way.

If in the end its easier to just move all the way to 2010 right off the bat thats fine but I need to look over all the libs i use to make sure they are ready for that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this