【发布时间】:2016-04-28 02:09:17
【问题描述】:
template <class... T_values>
class Thing {
public:
void something(T_values... values) {
tuple_ = std::tuple<T_values...>(values...);
}
void do_something_with_values() {
call_yadda_with_tuple(tuple_,
std::index_sequence_for<T_value...>())
}
void yadda(T... values);
private:
//The helper method.
template<std::size_t... Is>
void call_yadda_with_tuple(const std::tuple<T_values...>& tuple,
std::index_sequence<Is...>) {
yadda(std::get<Is>(tuple)...);
}
std::tuple<T_values...> tuple_;
};
以上源码来自:https://www.murrayc.com/permalink/2015/12/05/modern-c-variadic-template-parameters-and-tuples/
我想问一些问题:
- 什么返回
std::index_sequence_for<T_value...>())? - 为什么在
yadda(std::get<Is>(tuple)...);中有Is而不是Is...?因此,Is是什么意思?Is...在未打包(扩展)类型包中,但Is是什么。 - 特别是,
std::get适合 (1)-(8) (http://en.cppreference.com/w/cpp/utility/tuple/get) - 为什么
call_yadda_with_tuple得到std::index_sequence<Is...>。 毕竟,这种说法是无名的,所以没有用。我想它与扣除类型有关,但我看不出它有什么帮助?
【问题讨论】:
-
#2
std::get<Is>(tuple)...为Is...的每个值解包整个表达式 -
#3 的格式为 1-4(即
get<0>(tuple)将返回元组中的第一个条目,get<1>(tuple)将返回第二个,依此类推...)模板参数是一个数字给出要返回的元组条目的索引 - 注意:这是编译时间.. -
#4 函数本身的参数是多余的,推导的类型(提供索引序列是这里相关的,在这种情况下是数字序列
0,1,2,3等) -
std::index_sequence是size_t的特化std::integer_sequence。在这里阅读integer_sequence的提案可能会对您有所帮助open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html