小编urn*_*nav的帖子

使用std :: bind时,为什么复制ctor被调用了两次?

我正在玩,std::functionstd::bind了解如何复制参数,如果我可以保存一些复制操作.

我理解在使用时std::bind,参数是通过值传递而不是引用(除非std::ref指定).但是,当我运行以下代码段时,复制构造函数被调用两次.有人可以解释原因吗?

struct token
{
    static int i;
    int code;

    token()
        : code(i++)
    {
        cout << __FUNCTION__ << ": " << code << endl;
    }
    virtual ~token()
    {
        cout << __FUNCTION__ << endl;
    }

    token (token const & other)
        : code (other.code)
    {
        cout << "copy ctor: " << code << endl;
    }

    // update -- adding a move ctor
    token (token const && other)
        : code (std::move(other.code))
    {
        cout << "move …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind std-function

9
推荐指数
1
解决办法
229
查看次数

标签 统计

c++ ×1

std-function ×1

stdbind ×1