通过int检索可变类的给定成员
Retrieve given member of variadic class by int
这是一个递归类定义:
template<class... T>
class Mgr2
{
};
template<class T, class... args>
class Mgr2<T, args...>
{
Container<T> _container;
Mgr2<args...> _tail;
public:
Mgr2() { };
};
我想实现以下内容:
Mgr2<int, double> mgr;
mgr.get<0>(); // retrieves the Container<int> element
我怎么能这么做?
#include <type_traits>
template<class T>
struct Container {};
template<class... T>
class Mgr2
{
};
template<class T, class... args>
class Mgr2<T, args...>
{
Container<T> _container;
Mgr2<args...> _tail;
template<int idx>
auto _get(std::integral_constant<int, idx>) const
-> decltype(_tail.template get<idx-1>())
{ return _tail.get<idx-1>(); }
const Container<T> &_get(std::integral_constant<int, 0>) const
{ return _container; }
public:
Mgr2() { };
template<int idx>
auto get() const -> decltype(this->_get(std::integral_constant<int, idx>()))
{ return _get(std::integral_constant<int, idx>()); }
};
int main()
{
Mgr2<int, double> mgr;
mgr.get<0>(); // retrieves the Container<int> element
}
看看std::tuple
可能是个好主意。
相关文章:
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 枚举成员与静态 int 成员?
- 在类 (C++) 之外设置 const int 成员变量
- 错误:'class std::unique_ptr<std::set<long unsigned int> >'没有名为 'size' 的成员
- 错误:请求"数据"中的成员"大小",其非类类型为"int [5]"
- 'std::slice_array<int>'中没有命名'sum'的成员
- 对结构int成员进行排序
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 为什么我不能将 int 分配给结构成员的联合成员?
- 缺少类型说明符 - int 假定 声明静态成员后出错
- 参数包推导不一致 int 和 int& 在可变参数模板化成员函数中创建运行成员函数的线程
- 是否打印指向已定义 int 的成员指针
- 如何修复 Eigen::<int>NaturalOrdering>::MatrixType' 没有名为 'isCompressed' 的成员
- 无效使用非静态成员函数 int test::funcAB(int, int)
- 如何为包含map<int,*double>成员的类编写析构函数?
- 访问基类型数组成员(Int-to-type习惯用法)
- 通过指针递增成员 int 时,++ 与 -> 的运算符优先级如何工作?
- 为什么类的 reinterpret_cast<const char*> 及其预初始化成员 int 相同,而任何其他类型的变量都不是?
- 数据成员int引用