有幾種方法可以做到這一點。一種方法我想可以用這個模板整齊展開技巧:
auto l = {0, ((std::cout << args), 0)...};
(void)l;
的好處是,你不需要元組和其相對短相比其他方法。
如果您仍需要元組,你可以使用indicies通過索引來訪問的元組元素:
template<int... Indices>
struct indices {
typedef indices<Indices..., sizeof...(Indices)> next;
};
template<int N>
struct build_indices {
typedef typename build_indices<N - 1>::type::next type;
};
template<>
struct build_indices<0> {
typedef indices<> type;
};
template<int n>
using build_indices_t = typename build_indices<n>::type;
template<typename... Args>
class Functions
{
public:
Functions(Args... args)
: Functions(std::make_tuple(args...),
build_indices_t<sizeof...(Args)>())
{ }
private:
template<typename... Ts, int... Is>
Functions(const std::tuple<Ts...>& tup, indices<Is...>)
{
print(std::get<Is>(tup)...);
}
void print() { }
template<class Head, class... Tail>
void print(Head&& head, Tail&&... tail)
{ std::cout << head; print(tail...); }
};
我不這麼認爲。考慮到元組元素可以是不同的類型,那麼'* it'表達式應該是什麼類型?通常,您需要遞歸模板實例來遍歷參數包 - 您需要在編譯時「循環」,而不是在運行時。 – 2014-09-03 02:56:18
C++ 14使這更容易。 – Yakk 2014-09-03 03:01:16
有索引的'for'循環會爲您工作,而不是您的經典迭代器,但會循環使用相同的效果? – Niall 2014-09-03 05:37:38