`std :: unique_ptr`使得编译错误`候选构造函数(隐式复制构造函数)不可行

时间:2015-11-28 12:01:03

标签: c++ constructor static-methods unique-ptr

std::unique_ptr编译错误candidate constructor (the implicit copy constructor) not viable

让我通过以下代码声明它。

编译错误的代码是:

Code0:错误

#include <iostream>
class A {
public:
    static A str(){ return A();}
    std::unique_ptr<int> pi;
    A(): pi(new int(0)){}
};

错误是:

test.cpp:4:26: error: no matching constructor for initialization of 'A'
            static A str(){ return A();}
                                   ^~~
test.cpp:2:7: note: candidate constructor (the implicit copy constructor) not viable: expects an l-value for 1st argument
class A {
  ^
test.cpp:7:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
            A(){
            ^
1 error generated.

但下面的所有这些示例都是确定

代码1:确定

#include <iostream>
class A {
public:
    //static A str(){ return A();}
    std::unique_ptr<int> pi;
    A(): pi(new int(0)){}
};

Code2: ok

#include <iostream>
class A {
public:
    static A str(){ return A();}
    //std::unique_ptr<int> pi;
    A()/*: pi(new int(0))*/{}
};

Code3: ok

#include <iostream>
class A {
public:
    static A str(){ return A();}
    std::shared_ptr<int> pi;
    A(): pi(new int(0)){}
};

我正在使用g++,版本信息

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

有人可以解释一下吗?

0 个答案:

没有答案