数据成员"查询回调"不能是成员模板
Data member 'queryCallback' cannot be a member template
我在编译以下成员定义时遇到问题:
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;
}
输出:foo2 + 2 = 4
使用别名模板:
template<typename... B>
struct T
{
template<typename... A> using QueryCallbackType = std::tuple<std::string, std::function<void(A...)>>;
QueryCallbackType<B...> query_callback;
};
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么派生类的好友不能使用受保护的成员?
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 为什么"具有常量成员的结构"类型的指针不能指向"具有非常量成员的结构"?
- 为什么不能直接引用作用域枚举类成员,而不能为无作用域枚举生成类成员?
- 为什么静态数据成员不能在c++11中的类中初始化
- 为什么相应成员不能正确访问成员函数指针
- 为什么ofstream作为类成员不能传递给线程
- 为什么 constexpr 成员不能用于static_assert?
- C++:静态成员不能在声明时定义,但静态函数变量可以?
- 为什么类数据成员不能通过直接初始化语法初始化?
- 为什么静态常量成员不能出现在常量表达式中,例如 'switch'
- 私有静态成员不能从公共静态成员函数访问
- 为什么接口成员不能使用私有类类型
- c++:类的成员不能被识别
- 为什么当一个成员不能移动时,整个封闭类就不能移动
- 智能感知:类型 "const char *const []" 的成员不能具有类内初始值设定项
- 成员不能在当前作用域中定义
- 派生类中的成员不能使用c++受保护的函数