构造函数采用auto_ptr

cfh*_*cfh 0 c++ initialization auto-ptr

我想编写一个带有构造函数的C++类,该构造函数将其auto_ptr作为参数,以便我可以将类实例从auto_ptrs 初始化为另一个实例:

#include <memory>

class A
{
public:
  A() {}
  A(std::auto_ptr<A> other) {}
};

std::auto_ptr<A> create()
{
  return std::auto_ptr<A>(new A());
}

void foo()
{
  A x = create();
  // A y ( create() );    // works
}
Run Code Online (Sandbox Code Playgroud)

使用g++ -c test.cppgcc 4.6 编译此代码会产生以下错误消息:

test.cpp: In function ‘void foo()’:
test.cpp:17:16: error: no matching function for call to ‘std::auto_ptr<A>::auto_ptr(std::auto_ptr<A>)’
test.cpp:17:16: note: candidates are:
/usr/include/c++/4.6/backward/auto_ptr.h:260:7: note: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = A]
/usr/include/c++/4.6/backward/auto_ptr.h:260:7: note:   no known conversion for argument 1 from ‘std::auto_ptr<A>’ to ‘std::auto_ptr_ref<A>’
/usr/include/c++/4.6/backward/auto_ptr.h:125:9: note: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = A, _Tp = A]
/usr/include/c++/4.6/backward/auto_ptr.h:125:9: note:   no known conversion for argument 1 from ‘std::auto_ptr<A>’ to ‘std::auto_ptr<A>&’
/usr/include/c++/4.6/backward/auto_ptr.h:112:7: note: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = A, std::auto_ptr<_Tp> = std::auto_ptr<A>]
/usr/include/c++/4.6/backward/auto_ptr.h:112:7: note:   no known conversion for argument 1 from ‘std::auto_ptr<A>’ to ‘std::auto_ptr<A>&’
test.cpp:7:3: error:   initializing argument 1 of ‘A::A(std::auto_ptr<A>)’
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用语法A y (create());来创建我的对象,它就可以工作.

我想知道为什么会发生这种情况,如果有什么我可以做的解决它.

编辑:我还要指出,如果我将构造函数签名更改为

  A(const std::auto_ptr<A>& other) {}
Run Code Online (Sandbox Code Playgroud)

然后一切都很好地工作,但是这并没有取得所有权,auto_ptr因此没有我想要的语义.

编辑2:如果我使用赋值运算符执行相同的操作,即,

A& operator=( std::auto_ptr<A> other) {}
Run Code Online (Sandbox Code Playgroud)

那我就能做到

A x;
x = create();
Run Code Online (Sandbox Code Playgroud)

为什么?

Ker*_* SB 7

您只能进行一次隐式的,用户定义的转换.auto_ptr从另一个构造一个已经涉及通过辅助auto_ptr_ref类的隐式转换,因此你不能从一个隐式构造你自己的类auto_ptr.

通过使用直接初始化,其中一个转换是显式的,并且只保留一个隐式的用户定义转换,这很好.

要"解决"缺少隐式转换,您可以修改构造函数以获取auto_ptrby(非const)引用,或者将所有内容迁移到unique_ptrs.