在编译时生成零序列 [英] Generating a sequence of zeros at compile time
本文介绍了在编译时生成零序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题:
template< size_t... N_i >
class A
{
public:
// ...
void foo()
{
bar( /* 0,...,0 <- sizeof...(N_i) many */);
}
};
我想调用函数 bar
并向 sizeof ...(N_i)
传递许多全为零的参数,例如 bar(0,0,0)
,如果 sizeof ...(N_i)== 3
。
如何实现?
I want to call a function bar
and pass sizeof...(N_i)
many arguments to it which are all zeros, e.g., bar(0,0,0)
in case sizeof...(N_i) == 3
.
How can this be implemented?
推荐答案
bar(((void)N_i, 0)...);
逗号运算符将丢弃 N_i
只是右侧操作数的值( 0
)。强制转换是为了防止警告 N_i
被丢弃。
The comma operator will discard N_i
, yielding just the right-hand operand's value (0
). The cast is to prevent a warning about N_i
being discarded.
这篇关于在编译时生成零序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文