【发布时间】:2015-01-30 21:31:14
【问题描述】:
不久前,here 发布了打印 std::tuple 的解决方案。在大多数情况下,我了解正在发生的事情。不过,我无法理解 print_tuple 函数中发生的事情。
template<class Ch, class Tr, class Tuple, std::size_t... Is>
void print_tuple(std::basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){
using swallow = int[];
(void)swallow{0, (void(os << (Is == 0? "" : ", ") << std::get<Is>(t)), 0)...};
}
我不明白这个函数的主体中发生了什么。据我所知,这与解压Is 有关。我知道条件,Is == 0 正在检查我们是否在头元素。
那么发生了什么?
【问题讨论】:
-
代码从初始化列表构造(然后丢弃)
int[]数组,其中每个元素为 0,但打印元组的一个元素作为副作用(通过逗号运算符)。使用初始化列表只是为了进入包扩展可以工作的上下文。 -
啊!所以
swallow{...}构造是 int[] 的初始化列表。乍一看我没听懂。
标签: c++ c++11 tuples variadic-templates pretty-print