c++ - 与 unique_ptr 配对的正确方法是什么?

标签 c++ clang++

我正在尝试使用 unique_ptr make_pair,如下例所示

(该代码是外观设计模式的 C++ 实现)

#include <unordered_map>
#include <utility> 
#include <functional>

using namespace std;

// Step 1 : Design the Interface
class IAccount {
    protected: 
    int accountNo;
    int cash;

    public:
    virtual void deposit(float amount);
    virtual void withdraw(float amount);
    virtual void transfer(float amount);
    virtual int getAccountNumber() = 0;
};

// Step 2 : Implement the interface with one or more subtypes
class Current : public IAccount{
    public:
    Current(int amt) { cash = amt; }

    int getAccountNumber() {
        return accountNo;
    }
};

class Savings : public IAccount{
    public:
    Savings(int amt) { cash = amt; }

    int getAccountNumber() {
        return accountNo;
    }
};

// Step 3: Create the Facade class and wrap the classes that implement the Interface 
class BankService {
    unordered_map<int, unique_ptr<IAccount>> *bankAccounts;    
    int index;

    public: 
    BankService(): index(0){ 
        bankAccounts = new unordered_map<int, unique_ptr<IAccount>>();    
    }

    int createNewAccount(string name, int amount, string type, int accountNo) {
        unique_ptr<IAccount> newAccount;    
        if(type.compare("current")) {          
            newAccount.reset(new Current(amount));  
        }

        // Add to the unordered list --- Error here --- 
        pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, newAccount);
        bankAccounts->insert(toBeInserted); 

        return accountNo;
    }
};

代码吐出这个错误:

/Library/Developer/CommandLineTools/usr/include/c++/v1/vector:266:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:639:
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:634:12: error: no matching constructor for initialization of 'pair<typename
      __make_pair_return<int &>::type, typename __make_pair_return<unique_ptr<IAccount, default_delete<IAccount> > &>::type>' (aka 'pair<int,
      std::__1::unique_ptr<IAccount, std::__1::default_delete<IAccount> > >')
    return pair<typename __make_pair_return<_T1>::type, typename __make_pair_return<_T2>::type>
           ^

如果我把它改成:

// Add to the unordered list (note added const) 
pair<const int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, newAccount);
bankAccounts->insert(toBeInserted); 

欢迎我的是:

In file included from ./DesignPatterns/Structural/./../../Include/Common.h:1:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/vector:266:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/__bit_reference:15:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:640:
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:1783:31: error: call to implicitly-deleted copy constructor of 'std::__1::pair<const
      int, std::__1::unique_ptr<IAccount, std::__1::default_delete<IAccount> > >'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);

我尝试用 Clang++ 和 G++ 编译

Clang 版本:clang-900.0.39.2

G++ 版本:4.2.1

平台:OS x - x86_64-apple-darwin16.6.0

这是因为 Apple 提供的旧版 G++ 吗?

最佳答案

Is this due to the old version of G++ shipped by Apple?

没有。这是因为您试图通过将唯一指针传递给 make_pairinsert 来复制它。

std::unique_ptr 不可复制。事实上它只是移动。更改为:

pair<int, unique_ptr<IAccount>> toBeInserted = make_pair(accountNo, std::move(newAccount));
bankAccounts->insert(std::move(toBeInserted)); 

关于c++ - 与 unique_ptr 配对的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116065/

相关文章:

c++ - 包含在 MacOS 上使用 g++ 编译时未找到的路径

c++ - 使用静态 constexpr 成员(函数指针)的模板特化

c++ - 带有 O2 的 clang++ 的 undefined reference

c++ - 存储在变量模板特化中的 Spirit-X3 解析器不适用于 Clang

c++ - 指针++,未定义的行为

c++ - pop_back() 返回值?

c++ - clang 和 gcc 之间对三元运算符的 const 引用地址的差异

c++ - clang 左/右 3.6 倍表达式

c++ - 如何使用 Windows API C++ 更改已创建的文件夹/目录安全权限

c++ - 在 std::for_each 中使用 std::bind