#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的特殊R3)。)它是由两个编译错误:为什么它可以在GNU/C++中编译,无法在VC++ 2010 RTM中编译?
Description Resource Path 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
但它是正确的VS2010 RTM。
问题:
哪个编译立为ISO C++标准?
情况4的内容是问题“auto_ptr & auto_ptr_ref想要解决?”
您的意思是Visual Studio 2010候选版吗?它还没有RTMed。 – 2010-04-06 17:14:41
它不能用g ++ 4.4.1编译,至少在我删除了一些奇怪的东西之后 - 你能删除所有不会导致问题的代码,并且像#include“copy_of_auto_ptr.h”这样的东西。 – 2010-04-06 17:21:57