小编sof*_*mla的帖子

绑定可变参数模板参数时的副本太多

我正在创建一个工作队列.作业将在线程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) …
Run Code Online (Sandbox Code Playgroud)

c++ templates bind variadic-templates c++11

5
推荐指数
1
解决办法
271
查看次数

标签 统计

bind ×1

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1