C++ 返回auto_ptr在不同的gcc版本上有不同的结果

C++ 返回auto_ptr在不同的gcc版本上有不同的结果,c++,gcc,auto-ptr,C++,Gcc,Auto Ptr,我尝试使用gcc 4.4.2在QNX 6.5.0SP1上运行以下测试代码: // auto_ptr::operator= example #include <iostream> #include <cstdio> #include <memory> std::auto_ptr<std::string> source(void) { return std::auto_ptr<std::string>(new std::string(

我尝试使用gcc 4.4.2在QNX 6.5.0SP1上运行以下测试代码:

// auto_ptr::operator= example
#include <iostream>
#include <cstdio>
#include <memory>

std::auto_ptr<std::string> source(void)
{
  return std::auto_ptr<std::string>(new std::string("Hello World!\n"));
}

void sink(std::auto_ptr<std::string> arg)
{

}

int main () {
  std::auto_ptr<std::string> ap1 = source();// = ( new std::string("Hello World!\n"));
  std::auto_ptr<std::string> ap2 = source();
  std::printf("AP1 = %08d\nAP2 = %08d\n\n",ap1.get(), ap2.get());

   ap1 = source();
   std::printf("AP1 = %08d\nAP2 = %08d\n\n",ap1.get(), ap2.get());


  ap2 = ap1;
  std::printf("AP1 = %08d\nAP2 = %08d\n\n",ap1.get(), ap2.get());

  sink(ap2);
  std::printf("AP1 = %08d\nAP2 = %08d\n\n",ap1.get(), ap2.get());

  return 0;
}
//auto_ptr::operator=示例
#包括
#包括
#包括
标准::自动ptr源(无效)
{
返回std::auto_ptr(新std::string(“Hello World!\n”);
}
无效接收器(标准::自动参数)
{
}
int main(){
std::auto_ptr ap1=source();//=(新std::string(“Hello World!\n”);
std::auto_ptr ap2=source();
std::printf(“AP1=%08d\nAP2=%08d\n\n”,AP1.get(),ap2.get());
ap1=源();
std::printf(“AP1=%08d\nAP2=%08d\n\n”,AP1.get(),ap2.get());
ap2=ap1;
std::printf(“AP1=%08d\nAP2=%08d\n\n”,AP1.get(),ap2.get());
水槽(ap2);
std::printf(“AP1=%08d\nAP2=%08d\n\n”,AP1.get(),ap2.get());
返回0;
}
编译时出现以下错误:

helloworld_app.cpp:28: error: no match for 'operator=' in 'ap1 = source()()'
note: candidates are:
std::auto_ptr<_Ty>& std::auto_ptr<_Ty>::operator=(std::auto_ptr<_Other>&) [with _Other = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Ty = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
std::auto_ptr<_Ty>& std::auto_ptr<_Ty>::operator=(std::auto_ptr<_Ty>&) [with _Ty = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
std::auto_ptr<_Ty>& std::auto_ptr<_Ty>::operator=(std::auto_ptr_ref<_Ty>&) [with _Ty = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
helloworld\u app.cpp:28:错误:与'ap1=source()()'中的'operator='不匹配
注:候选人包括:
std::auto_ptr&std::auto_ptr::operator=(std::auto_ptr&)[带_Other=std::basic_string,_Ty=std::basic_string]
std::auto_ptr&std::auto_ptr::operator=(std::auto_ptr&)[带_Ty=std::basic_字符串]
std::auto_ptr&std::auto_ptr::operator=(std::auto_ptr_ref&)[带_Ty=std::basic_字符串]
我已经尝试通过cygwin在本地机器上使用gcc 5.4.0成功运行此代码,并且在本地机器上使用gcc 4.9.2。在gcc版本中,阻止函数返回值的自动\u ptr赋值与另一个本地自动\u ptr赋值是否存在差异?任何见解都会有所帮助

我已经看到了以下两个问题,并阅读了Herb Stutters在auto_ptrs上的帖子,我觉得我理解了他们所说的副本不平等,但我很困惑,为什么它在不同版本的gcc上表现出不同的行为


请注意,
auto_ptr
已被弃用,并在C++11中被
std::unique_ptr
std::shared_ptr
取代,假设您真的想使用
std::auto_ptr
,代码看起来很好。是的,如果我可以选择使用unique_ptr,这将是理想的情况,但是我的library.AnT不支持它,这就是造成混乱的原因。较新的编译器版本似乎运行正常,但我使用的特定版本却不正常。我不确定是否有公共问题列表,但我找不到一些基本的google fu。