[英]expansion parameters pack (values) from template arguments
there is a trying to expand the pack using usual way with recursion: 有一种尝试使用递归的常规方法来扩展包:
template<bool first> int func1(int value = 0) {
return some_func(first, value);
}
template<bool first, bool... args> int func1(int value = 0) {
return func1<args...>(some_func(first, value) );
}
at the last step of compile time recursion, the call of func1 is ambiguous, first candidate is a first function , it's clear , some concrete specialization in my case: 在编译时间递归的最后一步,func1的调用是模棱两可的,第一个候选对象是第一个函数,很明显,在我的情况下有一些具体的专长:
int func1(int) [with bool first = false]
int func1(int)[with bool first = false]
but second one is 但是第二个是
int func1(int) [with bool first = false;
int func1(int)[with bool first = false; bool ...args = {}]
bool ... args = {}]
you see that is also correct - empty set of the arguments after first one. 您会看到这也是正确的-在第一个参数之后留空一组参数。 any idea to prevent this ?
有什么想法可以防止这种情况吗?
thank you 谢谢
Disambiguate the base case from the recursive case by adding an explicit second
parameter: 通过添加一个显式的
second
参数来消除基本情况与递归情况的歧义:
template<bool first> int func1(int value = 0) {
return some_func(first, value);
}
template<bool first, bool second, bool... args> int func1(int value = 0) {
return func1<second, args...>(some_func(first, value) );
}
so the finally, I didn't use the recursion, but the only code below. 所以最后,我没有使用递归,而是下面的唯一代码。
(actually std::array is not required but more useful from my perspective , the expanding can be archived by using C-like array too) (实际上不需要std :: array,但从我的角度来看更有用,扩展也可以通过使用类似C的数组来存档)
template <bool... args> unsigned long func1() {
std::array<bool, sizeof...(args)> ar{args...};
// the piece specific for my task
std::bitset<sizeof...(args)> bs;
for(std::size_t i = 0; i < ar.size(); ++i) {
bs[i] = ar[i];
}
// ... processing ...
return bs.to_ulong();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.