是否可以为包中的每个参数声明一个方法
Is it possible to declare a method for each parameter in a pack?
例如:
template<class... keys>
struct{
{ virtual keys* getContents(foo* Foo) const = 0 }...;
}
或:
template<class... keys>
struct{
virtual bar* getBar( keys* Foo )... const = 0;
}
或者有类似效果的东西?
函数可以返回std::tuple
,不过:
template<class... keys>
struct something {
virtual std::tuple<keys...> getContents(foo* Foo) const = 0;
}
您也可以将此模板专门化为单个类型,并返回该类型,而不是一个单元素元组;并将其专门化为空参数包,并返回void
。
编辑:
你澄清了你的问题。你试图做一些不同的事情,你最初的解释有点离谱,这是可以理解的。
你可以完成你想要的,但它有点复杂,需要递归模板和专业化,但看起来这就是你想要的。
class bar;
template<typename ...Keys> struct getcontents_base;
template<>
struct getcontents_base<> {
};
template<typename firstKey, typename ...remainingKeys>
struct getcontents_base<firstKey, remainingKeys...>
: getcontents_base<remainingKeys...> {
virtual bar *getBar(firstKey *foo) const=0;
};
struct getcontents : public getcontents_base<int, char> {
bar *getBar(int *) const override {}
bar *getBar(char *) const override {}
};
struct notgetcontents : public getcontents_base<int, char> {};
void foo()
{
struct getcontents c; // This will compile fine.
struct notgetcontents c2; // This will result in a compilation error
// because the virtual methods have not
// been defined.
}
相关文章:
- 声明高维向量的更简洁的方法
- 模板方法访问正向声明的类仅在没有此指针的情况下无法编译
- 将子类方法声明为基类的友元
- 如何在声明为 const 的方法中更改类成员
- 编译器无法识别在类声明之外定义的方法
- 在类中声明 Array 成员的正确方法
- 如何在该接收类之外声明嵌套类的方法
- 转发声明在命名空间中不起作用的替代方法
- 在实现文件中使用模板参数声明方法
- 为什么在主方法之前声明方法会产生错误 c++
- 在 cpp 正文中声明方法时未定义的符号
- 声明方法时没有名称空间,也没有对C 作用的方法的对象
- 无法在类中声明方法 (C++_
- 可变声明方法
- 为什么这种前向声明方法会产生错误
- 在c++中可以通过模板声明方法吗?
- 在c++中声明方法时= 0的含义是什么?
- 为什么我需要为部分模板专门化重新声明方法/成员?
- 两种数组声明方法的区别
- 强制编译器声明方法是否已被实现- c++