C++ 为什么';可以用GNU/C++;,can';t在VC+中编译+;2010 RTM? #包括 #包括 #包括 #包括“自动打印的副本” #ifdef硕士学位 #pragma消息(“包含”) #包括 // http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-布拉格马 #恩迪夫 /* 情况1-4是自动ptr的要求。 哪种形式http://ptgmedia.pearsoncmg.com/images/020163371X/autoptrupdate/auto_ptr_update.html */ /* 案例1。 (1) 直接初始化,相同类型,例如。 */ std::auto_ptr source_int(){ //返回std::auto_ptr(新int(3)); 标准:自动ptr tmp(新int(3)); 返回tmp; } /* 案例2。 (2) 复制初始化,相同类型,例如。 */ 无效接收点(标准::自动接收){ std::cout
我认为缩写版本是:C++ 为什么';可以用GNU/C++;,can';t在VC+中编译+;2010 RTM? #包括 #包括 #包括 #包括“自动打印的副本” #ifdef硕士学位 #pragma消息(“包含”) #包括 // http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-布拉格马 #恩迪夫 /* 情况1-4是自动ptr的要求。 哪种形式http://ptgmedia.pearsoncmg.com/images/020163371X/autoptrupdate/auto_ptr_update.html */ /* 案例1。 (1) 直接初始化,相同类型,例如。 */ std::auto_ptr source_int(){ //返回std::auto_ptr(新int(3)); 标准:自动ptr tmp(新int(3)); 返回tmp; } /* 案例2。 (2) 复制初始化,相同类型,例如。 */ 无效接收点(标准::自动接收){ std::cout,c++,gnu,auto-ptr,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-Pra
#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;
}
注意复制构造函数(来自非常量引用)的不寻常形式,它阻止(根据标准,GCC是正确的)从临时复制构造实例的能力(来自make()
)
这种情况要复杂得多,因为
std::auto_ptr
试图用包装器auto_ptr_ref
来解决由此产生的限制。然而,由于您还想更改指针的类型,它可能在某个地方由于所有这些隐式转换而发生故障,VC++仅在感谢的情况下才设法编译它到非标准扩展(允许将右值绑定到非常量引用)
编译器实际上正确地告诉我。在问题行:
struct X
{
X() {}
X(X&);
};
X make() { return X(); }
void receive(X ) { }
int main()
{
receive(make());
}
我认为缩短的版本是:
#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;
}
注意复制构造函数(来自非常量引用)的不寻常形式,它阻止(根据标准,GCC是正确的)从临时复制构造实例的能力(来自make()
)
这种情况要复杂得多,因为
std::auto_ptr
试图用包装器auto_ptr_ref
来解决由此产生的限制。然而,由于您还想更改指针的类型,它可能在某个地方由于所有这些隐式转换而发生故障,VC++仅在感谢的情况下才设法编译它到非标准扩展(允许将右值绑定到非常量引用)
编译器实际上正确地告诉我。在问题行:
struct X
{
X() {}
X(X&);
};
X make() { return X(); }
void receive(X ) { }
int main()
{
receive(make());
}
你是说Visual Studio 2010候选版本吗?它还没有RTMed。它不能用g++4.4.1编译,至少在我删除了一些奇怪的东西之后-你能删除所有不会导致问题的代码和类似的东西吗。你是说Visual Studio 2010候选版本吗?它还没有RTMed。它不能用g++4.4.1编译,至少在我删除了一些奇怪的东西之后-你能删除所有不会引起问题的代码和类似的东西吗?包括“复制”of _auto _ptr.h.>你是说Visual Studio 2010的候选版本吗?它还没有RTMed。-詹姆斯·麦克内利斯8小时前不是RC版本,//它是一个RTM版本,将来会发布,但现在是私有的。但是的版本是在1994年编写的。它不能用g++4.4.1编译,至少在我删除一些奇怪的东西之后-你能删除所有吗不会引起问题的代码和类似的东西包括“copy_of_auto_ptr.h”–Neil Butterworth 8小时前//对不起,我手工复制了我的代码,我忘了从我发布的代码中删除它。事实上,auto_ptr的副本与auto_ptr相同,它将STL的源代码复制为auto_ptr的副本,而不做任何更改。它的行为是正确的。>你指的是Visual Studio 2010发布候选版本吗?它是还没有RTMed。-James McNellis 8小时前不是RC版本,//这是一个RTM版本,将在将来发布,但现在是私有的。但是的版本是在1994年编写的。它不能用g++4.4.1编译,至少在我删除一些奇怪的东西之后-你能删除所有不会引起问题的代码和类似#include#的东西吗“copy_of_auto_ptr.h”–Neil Butterworth 8小时前//对不起,我手工复制了我的代码,我忘了从我发布的代码中删除它。事实上,_auto_ptr的副本与auto_ptr相同,它将STL的源代码替换为“auto_ptr”而不做任何更改。它的行为是正确的。