我在爲了好玩而在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 &&不是你可以移動的東西,然後你也不會從任何東西移動。 – Barry