绑定可变模板参数时副本过多
Too many copies when binding variadic template arguments
我正在创建作业队列。作业将在线程 A 中创建,然后作业将发送到线程 B,线程 B 将执行该作业。作业完成后,作业将被发送回线程A。
#include <functional>
#include <iostream>
#include <memory>
using namespace std;
template<typename T, typename... Args>
class Job
{
public:
Job(std::weak_ptr<T> &&wp, std::function<void(const Args&...)> &&cb)
: _cb(std::move(cb)),
_cbWithArgs(),
_owner(std::move(wp)) {}
public:
template<typename... RfTs>
void bind(RfTs&&... args)
{
// bind will copy args for three times.
_cbWithArgs = std::bind(_cb, std::forward<RfTs>(args)...);
}
void fire()
{
auto sp = _owner.lock();
if (sp)
{
_cbWithArgs();
}
}
private:
std::function<void(const Args& ...)> _cb;
std::function<void()> _cbWithArgs;
std::weak_ptr<T> _owner;
};
struct Args
{
Args() = default;
Args(const Args &args)
{
cout << "Copied" << endl;
}
};
struct Foo
{
void show(const Args &)
{
cout << "Foo" << endl;
}
};
int main()
{
using namespace std::placeholders;
shared_ptr<Foo> sf (new Foo());
Args args;
// Let's say here thread A created the job.
Job<Foo, Args> job(sf, std::bind(&Foo::show, sf.get(), _1));
// Here thread B has finished the job and bind the result to the
// job.
job.bind(args);
// Here, thread A will check the result.
job.fire();
}
以上代码可以编译并运行。但它给出了以下结果(g++ 4.8.4 & clang 具有相同的结果):
Copied
Copied
Copied
Foo
一共三份!不能接受,我不知道我哪里做错了。为什么是三本?我用谷歌搜索并从这里找到了一种方法:,它只复制一次参数。但是它必须在构造函数中初始化绑定函数。
谢谢 Piotr Skotnicki。不幸的是,我没有 C++14 编译器。那么,让我们让 Args 可移动:
struct Args
{
Args() = default;
Args(const Args &args)
{
cout << "Copied" << endl;
}
Args(Args &&) = default;
Args& operator=(Args &&) = default;
};
现在,它只复制一次:)
最后,我采用了这个线程中的代码。模板 gen_seq 是真正的艺术,我必须说。
第一个副本由 std::bind
自己制作:
std::bind(_cb, std::forward<RfTs>(args)...)
因为它需要存储其参数的衰减副本。
另外两个副本由 _cbWithArgs
的赋值生成,类型为 std::function
(§ 20.8.11.2.1 [func.wrap.func.con]):
template<class F> function& operator=(F&& f);
18 Effects: function(std::forward<F>(f)).swap(*this);
其中:
template<class F> function(F f);
9 [...] *this
targets a copy of f
initialized with std::move(f)
.
即一个构造函数的参数副本,另一个用于存储参数f
。
由于 Args
是不可移动的类型,尝试从 移动 调用 std::bind
的结果回退到副本。
在您的代码中将作业执行的结果存储在 std::function
中似乎不合理。相反,您可以将这些值存储在一个元组中:
template <typename T, typename... Args>
class Job
{
public:
Job(std::weak_ptr<T> wp, std::function<void(const Args&...)> &&cb)
: _cb(std::move(cb)),
_owner(wp) {}
public:
template<typename... RfTs>
void bind(RfTs&&... args)
{
_args = std::forward_as_tuple(std::forward<RfTs>(args)...);
}
void fire()
{
auto sp = _owner.lock();
if (sp)
{
apply(std::index_sequence_for<Args...>{});
}
}
private:
template <std::size_t... Is>
void apply(std::index_sequence<Is...>)
{
_cb(std::get<Is>(_args)...);
}
std::function<void(const Args&...)> _cb;
std::weak_ptr<T> _owner;
std::tuple<Args...> _args;
};
我正在创建作业队列。作业将在线程 A 中创建,然后作业将发送到线程 B,线程 B 将执行该作业。作业完成后,作业将被发送回线程A。
#include <functional>
#include <iostream>
#include <memory>
using namespace std;
template<typename T, typename... Args>
class Job
{
public:
Job(std::weak_ptr<T> &&wp, std::function<void(const Args&...)> &&cb)
: _cb(std::move(cb)),
_cbWithArgs(),
_owner(std::move(wp)) {}
public:
template<typename... RfTs>
void bind(RfTs&&... args)
{
// bind will copy args for three times.
_cbWithArgs = std::bind(_cb, std::forward<RfTs>(args)...);
}
void fire()
{
auto sp = _owner.lock();
if (sp)
{
_cbWithArgs();
}
}
private:
std::function<void(const Args& ...)> _cb;
std::function<void()> _cbWithArgs;
std::weak_ptr<T> _owner;
};
struct Args
{
Args() = default;
Args(const Args &args)
{
cout << "Copied" << endl;
}
};
struct Foo
{
void show(const Args &)
{
cout << "Foo" << endl;
}
};
int main()
{
using namespace std::placeholders;
shared_ptr<Foo> sf (new Foo());
Args args;
// Let's say here thread A created the job.
Job<Foo, Args> job(sf, std::bind(&Foo::show, sf.get(), _1));
// Here thread B has finished the job and bind the result to the
// job.
job.bind(args);
// Here, thread A will check the result.
job.fire();
}
以上代码可以编译并运行。但它给出了以下结果(g++ 4.8.4 & clang 具有相同的结果):
Copied
Copied
Copied
Foo
一共三份!不能接受,我不知道我哪里做错了。为什么是三本?我用谷歌搜索并从这里找到了一种方法:,它只复制一次参数。但是它必须在构造函数中初始化绑定函数。
谢谢 Piotr Skotnicki。不幸的是,我没有 C++14 编译器。那么,让我们让 Args 可移动:
struct Args
{
Args() = default;
Args(const Args &args)
{
cout << "Copied" << endl;
}
Args(Args &&) = default;
Args& operator=(Args &&) = default;
};
现在,它只复制一次:)
最后,我采用了这个线程中的代码。模板 gen_seq 是真正的艺术,我必须说。
第一个副本由 std::bind
自己制作:
std::bind(_cb, std::forward<RfTs>(args)...)
因为它需要存储其参数的衰减副本。
另外两个副本由 _cbWithArgs
的赋值生成,类型为 std::function
(§ 20.8.11.2.1 [func.wrap.func.con]):
template<class F> function& operator=(F&& f);
18 Effects:
function(std::forward<F>(f)).swap(*this);
其中:
template<class F> function(F f);
9 [...]
*this
targets a copy off
initialized withstd::move(f)
.
即一个构造函数的参数副本,另一个用于存储参数f
。
由于 Args
是不可移动的类型,尝试从 移动 调用 std::bind
的结果回退到副本。
在您的代码中将作业执行的结果存储在 std::function
中似乎不合理。相反,您可以将这些值存储在一个元组中:
template <typename T, typename... Args>
class Job
{
public:
Job(std::weak_ptr<T> wp, std::function<void(const Args&...)> &&cb)
: _cb(std::move(cb)),
_owner(wp) {}
public:
template<typename... RfTs>
void bind(RfTs&&... args)
{
_args = std::forward_as_tuple(std::forward<RfTs>(args)...);
}
void fire()
{
auto sp = _owner.lock();
if (sp)
{
apply(std::index_sequence_for<Args...>{});
}
}
private:
template <std::size_t... Is>
void apply(std::index_sequence<Is...>)
{
_cb(std::get<Is>(_args)...);
}
std::function<void(const Args&...)> _cb;
std::weak_ptr<T> _owner;
std::tuple<Args...> _args;
};