如何将可变参数传递给虚函数

ser*_*rup 5 c++ variadic-functions std-function

我有一个add使用以下设置的工作虚拟功能:

using Func = std::function<std::vector<unsigned char>()>;      

class cfExecutor {
public:
    cfExecutor();
    virtual ~cfExecutor();
    /// Enqueue a function to be executed by this executor. This and all                     
    /// variants must be threadsafe.
    virtual void add(Func) = 0;                              
    virtual void add(std::vector<Variant> params, Func callback) = 0; 
};


class ManualExecutor : public cfExecutor                                                                            
{                                                                                                                   

    std::mutex lock_;             // avoid multiple entity updating the function container                          
    std::queue<Func> funcs_;      // functions queued for running                                                 

    public:                                                                                                         
    std::map<int8_t, Func> funcs; // Function container (priority,Func) - added functions to this Executors       

    ManualExecutor() {}                                                                                             
    ManualExecutor(ManualExecutor&& other):                                                                         
                    funcs(std::move(other.funcs))                                                                   
    {}                                                                                                              
    ~ManualExecutor() {}                                                                                            

    void add(Func callback);                                                                          
    void add(std::vector<Variant> params, Func callback);                     
};                                                                                                                  
Run Code Online (Sandbox Code Playgroud)

然后我想向函数添加可变参数 - 像这样:

using Func = std::function<std::vector<unsigned char>(const auto&...args)>;  
Run Code Online (Sandbox Code Playgroud)

但是我得到了隐式错误 [隐式模板可能不是“虚拟”]

我应该如何使用可变参数定义 add 函数?

到目前为止,我已经使用以下方法解决了它:

using Func = std::function<std::vector<unsigned char>(std::vector<Variant>)>;
Run Code Online (Sandbox Code Playgroud)

然后让 lambda 函数处理来自向量内部的接收参数 - 像这样:

auto lambdaFunction = [](std::vector<Variant> vec){        

    std::vector<unsigned char> result;                     
    cout << "- Hello from executor - lambdaFunction ";     
    std::cout << ": parameters : ";                        
    for(auto a: vec) {                                     
        std::cout << a << ",";                             
    }                                                      
    std::cout << std::endl;                                 

    std::string s("this is the result");                   
    result.insert(result.end(),s.begin(), s.end());        
    return result;                                         
};        
Run Code Online (Sandbox Code Playgroud)

n. *_* m. 2

看起来您想传递一个绑定函数。

这可以通过 来完成std::bind。您不需要add以任何方式进行更改或添加vector<variant>基于 - 的重载。只需按如下方式调用即可:

std::vector<unsigned char> myfunction (int, const char*, Foo&);

...
int i = 123;
const char* s = "abc";
Foo foo{1,2,3};
executor->add(std::bind(myfunction, i, s, foo));
Run Code Online (Sandbox Code Playgroud)

将其与常规函数、成员函数、类函数对象(“函子”)或 lambda 一起使用。对于内联 lambda 来说,它没有多大意义,因为您可以捕获 lambda 本身中的数据。

executor->add([&i,s,&foo](){ ... use(i); use(s); use(foo); }
Run Code Online (Sandbox Code Playgroud)

事实上,您始终可以bind用 a 替换表达式,lambda这可以使我更具可读性。比较:

int foo;
std::bind(std::operator<<, std::cout, foo);
Run Code Online (Sandbox Code Playgroud)

[foo]() { return std::cout << foo; }
Run Code Online (Sandbox Code Playgroud)