gpt4 book ai didi

c++ - 数据成员 'queryCallback' 不能是成员模板

转载 作者:行者123 更新时间:2023-11-30 04:04:29 24 4
gpt4 key购买 nike

我在编译以下成员定义时遇到问题:

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

错误如题:

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

我意识到我不能为不是方法/函数的成员使用模板定义。考虑到这一点,我该如何使用 <A...>在这种情况下?

谢谢。

最佳答案

有根据的猜测,您需要将其定义为模板别名:

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

代码示例:

#include <tuple>
#include <iostream>
#include <string>
#include <functional>

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

int main()
{
auto foo = [](int a, int b) { std::cout << a << " + " << b << " = " << a + b << std::endl; };
queryCallback<int, int> A("foo", foo);

std::cout << std::get<0>(A) << std::endl;
std::get<1>(A)(2, 2);

return 0;
}

输出:

foo

2 + 2 = 4

关于c++ - 数据成员 'queryCallback' 不能是成员模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23715830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com