tags:

views:

234

answers:

1
#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;
}

In Eclipse(GNU C++.exe -v gcc version 3.4.5 (mingw-vista special r3)) it's two compile error:

Description Resource Path Location Type initializing argument 1 of void sink_base(std::auto_ptr<Base>)' from result ofstd::auto_ptr<_Tp>::operator std::auto_ptr<_Tp1>() [with _Tp1 = Base, _Tp = Derived]' auto_ptr_ref_research.cpp auto_ptr_ref_research/auto_ptr_ref_research 190 C/C++ Problem

Description Resource Path Location Type no matching function for call to `std::auto_ptr::auto_ptr(std::auto_ptr)' auto_ptr_ref_research.cpp auto_ptr_ref_research/auto_ptr_ref_research 190 C/C++ Problem

But it's right in VS2010 RTM.

Questions:

  1. Which compiler stand for the ISO C++ standard?

  2. The content of case 4 is the problem "auto_ptr & auto_ptr_ref want to resolve?"

+7  A: 

I think a shortened version is:

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

X make() { return X(); }

void receive(X ) { }

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

Note the unusual form of copy constructor (from a non-const reference) which prevents (by standard, GCC is correct) the ability to copy-construct an instance from a temporary (the result of make()).


The situation is way more complicated because std::auto_ptr attempts to work around with the resulting limitations with a wrapper auto_ptr_ref. However, since you also want to change the type of the pointer, it probably breaks down somewhere with all those implicit conversions and VC++ manages to compile it only thanks to a nonstandard extension (allowing binding rvalues to non-constant references).

The compiler actually tells me right that. On the problem line:

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

Anyway, std::auto_ptr is a bit of a failed experiment with bizarre semantics, and deprecated in the next standard. In C++0x (e.g with gcc 4.4.1) it would work if you replaced all occurrences of auto_ptr with unique_ptr, and changed the signature of sink functions to use rvalue references to get the ownership transferring.

void sink_base( std::unique_ptr<Base>&& p);
UncleBens
> Do you mean the Visual Studio 2010 release candidate? It has not RTMed yet. – James McNellis 8 hours ago not the RC version, //it's a RTM version which will published in future but it's private now. but the version of <memory> is written in 1994.
Eric Kung
It can't be compiled with g++ 4.4.1, at least after I remove some strange stuff - could you remove all the code that isn't causing problems and stuff like #include "copy_of_auto_ptr.h". – Neil Butterworth 8 hours ago// sorry, i copy my code by hand, i forgot to remove it from my published code. In fact, the copy_of_auto_ptr is the same as auto_ptr, it copy the source code of STL replace 'auto_ptr' to 'copy_of_auto_ptr' without any change. it's behavior is correct.
Eric Kung