【发布时间】:2016-05-09 18:56:49
【问题描述】:
我在 C++ 中实现元组是为了好玩,但由于下面的类对其进行了修改,因此我可以轻松地使用模板提取每个元素,只使用索引来提取每个元素。类似于 std::get。
TL;DR:提取元组内元素的实现是什么样的?
template<typename first_t, typename ... arg_v>
class Tuple : public Tuple<arg_v ...>
{
public:
Tuple()
{
}
Tuple(const first_t&& A) :
element(A)
{
}
Tuple(const first_t& A, arg_v& ... args) :
element(A), Tuple<arg_v ...>::Tuple(args ...)
{
}
Tuple(const first_t&& A, arg_v&& ... args) :
element(A), Tuple<arg_v ...>::Tuple(args ...)
{
}
first_t element;
};
template<typename last_t>
class Tuple<last_t>
{
public:
Tuple()
{
}
Tuple(const last_t& A) :
element(A)
{
}
Tuple(const last_t&& A) :
element(A)
{
}
last_t element;
};
【问题讨论】:
-
可以看boost::tuple或者std::tuple的源码进行指导。
-
你的移动构造函数不正确 -
const&&不是你可以移动的东西,然后你无论如何也不能移动任何东西。
标签: c++ templates tuples generic-programming