VC ++ 6.0 auto_ptr模板类到VS2008/VS2010的转换问题 [英] Conversion issue with VC++6.0 auto_ptr template class to VS2008/VS2010

查看:102
本文介绍了VC ++ 6.0 auto_ptr模板类到VS2008/VS2010的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在转换我们的一些第三方源代码模块(使用 VC ++ 6.0 编写/构建),使其也可以在 VS2008/2010 中构建.使用较新的编译器,还更加严格地制定了一些C ++规则.
不幸的是,我现在遇到基于 auto_ptr 类模板的与自己的类模板有关的转换问题.更确切地说是:复制构造函数.
自己实现的auto_ptr模板的头文件:

Hello all,

I''m in process converting some of our 3rd party source modules (written/build with VC++ 6.0) to make them build within VS2008/2010 too. With the newer compilers also some C++ rules have been made more strictly.
Unfortunately I''m now running into conversion issue related to an own class template based around the auto_ptr class template. And to be more exactly: the copy constructor.
The headerfile of the own implemented auto_ptr template:

#ifndef __MY_AUTO_PTR_H
#define __MY_AUTO_PTR_H
#include <vector>
template <class T>
class my_auto_ptr : public std::auto_ptr<T>
{
public:
    my_auto_ptr() : std::auto_ptr<T>(){}
    my_auto_ptr(T* p):std::auto_ptr<T>(p){}
my_auto_ptr(const my_auto_ptr & lp)   :std::auto_ptr<T>(lp){ }
    bool operator < (const std::auto_ptr<T> &pt) const { return *get() < *pt ;}
    bool operator == (const std::auto_ptr<T> &pt) const { return *get() == *pt ;}
};
#endif


C ++编译器对bool运算符上方的``my_auto_ptr(const ...'')进行故障转移.
编译器向我抛出下一条错误消息:


The C++ compiler fails over the ''my_auto_ptr(const...'' above the bool operator.
The compiler throws next error message to me:

c:\...\my_auto_ptr.h(19) : error C2664: 'std::auto_ptr<_Ty>::auto_ptr<T>(std::auto_ptr<_Ty> &) throw()' : cannot convert parameter 1 from 'const my_auto_ptr<T>' to 'std::auto_ptr<_Ty> &'
        with
        [
            _Ty=CLine,
            T=CLine
        ]
        and
        [
            T=CLine
        ]
        and
        [
            _Ty=CLine
        ]
        Conversion loses qualifiers
        c:\...\my_auto_ptr.h(19) : while compiling class template member function 'my_auto_ptr<T>::my_auto_ptr(const my_auto_ptr<T> &)'
        with
        [
            T=CLine
        ]
        C:\Program Files\Microsoft Visual Studio 9.0\VC\atlmfc\include\afxtempl.h(1257) : see reference to class template instantiation 'my_auto_ptr<T>' being compiled
        with
        [
            T=CLine
        ]
Etc...




恐怕现在我有点迷路了...

你们有建议吗?
提前谢谢,
EiSl




I''m afraid that I''m a bit lost here by now...

Do you guys have a suggestion?
Thx in advance,
EiSl

推荐答案

问题是std :: auto_ptr没有标准副本构造函数".

auto_ptr 复制的原型是
The problem is that std::auto_ptr does not have a "standard copy constructor".

The prototype for auto_ptr copy is
auto_ptr(auto_ptr&)


(请注意非const参数).

您正在将const 传递给旨在对其进行修改的内容.

正确的方法是使用


(note the non const parameter).

You are passing a const to something that is designed to modify it.

The proper way to do this is having a

//non-const reference
my_autoptr(myautoptr& s) :auto_ptr(s) {}

//pure value (relies of auto_ptr::operator ref() )
my_autoptr(typename auto_ptr::ref s) :auto_ptr(s) {} 



注意:auto_ptr没有虚方法,所以不要使用auto_ptrmy_autoptr多态.



Note: auto_ptr doesn''t have virtual methods, so don''t use auto_ptr and my_autoptr polymorphicaly.


Nicolai Josuttis在他的书"The C ++ Standard Library"(本节)中4.2 on class auto_ptr)说:根据auto_ptrs的概念,可以通过使用常量引用将所有权转移到函数中.这非常危险,因为人们通常希望对象在传递时不会被修改.幸运的是,有一个较晚的设计决策使auto_ptrs的危险性降低了;通过一些棘手的实现技术,使用常量引用无法实现所有权的转移.实际上,您无法更改常量auto_ptr的所有权. .."

如果我正确地理解了Josuttis中的讨论,那么看来您可能已经对编译器进行了改进,使其变得更加严格,并且使您的源代码也需要进行更改.

Josuttis的书似乎是STL的很好的参考书,并且普遍的共识是,它属于每个C ++程序员的书架.
Nicolai Josuttis in his book "The C++ Standard Library" (section 4.2 on Class auto_ptr) says "According to the concept of auto_ptrs, it is possible to transfer ownership into a function by using a constant reference. This is very dangerous because people usually expect that an object won''t get modified when you pass it as a constant reference. Fortunately, there was a late design decision that made auto_ptrs less dangerous. By some tricky implementation techniques, transfer of ownership is not possible with constant references. In fact you can''t change the ownership of an constant auto_ptr..."

If I understand this discussion in Josuttis correctly, it appears that you may have run into an improvement of the compiler, making it more strict and also making your source requiring a change.

The Josuttis book seems to be a pretty good reference on the STL and the general consensus is that it belongs on every C++ programmer''s bookshelf.


这篇关于VC ++ 6.0 auto_ptr模板类到VS2008/VS2010的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆