为什么它可以用GNU / C ++编译,无法在VC ++ 2010 RTM中编译?

时间:2010-04-06 17:10:31

标签: c++ gnu auto-ptr

#include <stdlib.h>
#include <iostream>
#include <memory>
#include "copy_of_auto_ptr.h"
#ifdef _MSC_VER
#pragma message("#include <string>")
#include <string>
// http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
#endif

/*
 case 1-4 is the requirement of the auto_ptr.
 which form http://ptgmedia.pearsoncmg.com/images/020163371X/autoptrupdate/auto_ptr_update.html
*/
/*
 case 1.
 (1) Direct-initialization, same type, e.g.
*/
std::auto_ptr<int> source_int() {
    // return std::auto_ptr<int>(new int(3));
    std::auto_ptr<int> tmp(new int(3));
    return tmp;
}

/*
 case 2.
 (2) Copy-initialization, same type, e.g.
*/
void sink_int(std::auto_ptr<int> p) {
    std::cout << "sink_int << " << *p << std::endl;
}

/*
 case 3.
 (3) Direct-initialization, base-from-derived, e.g.
*/

class Base {
public:
    Base() {
        std::cout << "creating Base object..." << std::endl;
    }
    virtual ~Base(){
        std::cout << "destoring Base object..." << std::endl;
    }
    virtual void go(){
        std::cout << "Base::go()" << std::endl;
    }
};

class Derived : public Base {
public:
    Derived() {
        std::cout << "creating Derived object..." << std::endl;
    }
    ~Derived(){
        std::cout << "destoring Derived object..." << std::endl;
    }
    void go(){
        std::cout << "Derived::go()" << std::endl;
    }
};

std::auto_ptr<Derived> source_derived() {
    // return std::auto_ptr<Derived>(new Derived());
    std::auto_ptr<Derived> tmp(new Derived());
    return tmp;
}

/*
 case 4.
 (4) Copy-initialization, base-from-derived, e.g.
*/
void sink_base( std::auto_ptr<Base> p) {
    p->go();
}

int main(void)
{
    /*
    // auto_ptr
    */
    // case 1. // auto_ptr
    std::auto_ptr<int> p_int(source_int());
    std::cout << *p_int << std::endl;

    // case 2. // auto_ptr
    sink_int(source_int());

    // case 3. // auto_ptr
    std::auto_ptr<Base> p_derived(source_derived());
    p_derived->go();

    // case 4. // auto_ptr
    sink_base(source_derived());

    return 0;
}

在Eclipse(GNU C ++。exe -v gcc 3.4.5版(mingw-vista special r3))中有两个编译错误:

  

描述资源路径位置类型     初始化void sink_base(std::auto_ptr<Base>)' from result of的参数1 std :: auto_ptr&lt; _Tp&gt; :: operator std :: auto_ptr&lt; _Tp1&gt;()[with _Tp1 = Base,_Tp = Derived]'auto_ptr_ref_research.cpp auto_ptr_ref_research / auto_ptr_ref_research 190 C / C ++问题

     

描述资源路径位置类型   没有匹配函数来调用`std :: auto_ptr :: auto_ptr(std :: auto_ptr)'auto_ptr_ref_research.cpp auto_ptr_ref_research / auto_ptr_ref_research 190 C / C ++问题

但它在VS2010 RTM中是正确的。

问题:

  1. 哪个编译器代表ISO C ++标准?

  2. 案例4的内容是“auto_ptr&amp; auto_ptr_ref想要解决的问题?”

1 个答案:

答案 0 :(得分:7)

我认为缩短版本是:

struct X
{
    X() {}
    X(X&);
};

X make() { return X(); }

void receive(X ) { }

int main()
{
    receive(make());
}

注意复制构造函数的异常形式(来自非const引用),它阻止(通过标准,GCC是正确的)从临时(make()的结果)复制构造实例的能力。


情况变得更加复杂,因为std::auto_ptr尝试使用包装器auto_ptr_ref解决由此产生的限制。但是,由于您还希望更改指针的类型,它可能会在所有这些隐式转换中出现故障,并且由于非标准扩展(允许将rvalues绑定到非常量引用),VC ++设法编译它。

编译器实际上告诉了我。在问题上:

warning C4239: nonstandard extension used : 'argument' : 
conversion from 'std::auto_ptr<_Ty>' to 'std::auto_ptr<_Ty> &' 

无论如何,std::auto_ptr对于奇怪的语义来说是一个失败的实验,并在下一个标准中被弃用。在C ++ 0x中(例如使用gcc 4.4.1),如果用auto_ptr替换所有出现的unique_ptr,它会起作用,并更改了接收器函数的签名以使用右值引用来获取所有权转移

void sink_base( std::unique_ptr<Base>&& p);