包含可变模板的元组

Tuple which includes a variadic template

本文关键字:元组 包含可      更新时间:2023-10-16

我有以下代码:

template<typename... A>
void queueQuery(std::string query, std::function<void(A...)> callback = nullptr);
template<typename... A>
std::tuple<std::string, std::function<void(A...)> queryCallback;
std::queue<queryCallback> queryQueue;

我想创建一个字符串和函数的元组队列,这些字符串和函数具有任意给定类型的可变数(我理解,这很复杂(。

有没有办法做到这一点?

我目前得到以下错误:

/databasedispatcher.h:14: error: data member 'queryCallback' cannot be a member template

您正在定义queryCallback

template<typename... A> 
std::tuple<std::string, std::function<void(A...)> queryCallback;

作为一个变量模板(仅符合C++14(。你确定这就是你想要的吗?

如果你想要严格的C++11合规性,那么把你的变量包装成一个结构,如下所示:

template<typename... A> 
struct Foo{
    std::tuple<std::string, std::function<void (A...)> > queryCallback;
};

然后将其用作临时中的do_something(Foo<int, double>().queryCallback);,或者Foo<int, double> foo; do_something(foo.queryCallback);希望这能有所帮助。