Why it's can be compiled in GNU/C++, can't compiled in VC++2010 RTM?
#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 Pat开发者_StackOverflow社区h Location Type initializing argument 1 of
void sink_base(std::auto_ptr<Base>)' from result of
std::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++ ProblemDescription 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:
Which compiler stand for the ISO C++ standard?
The content of case 4 is the problem "auto_ptr & auto_ptr_ref want to resolve?"
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);
精彩评论