template <bool ...T>
int some_function()
{
// this is the function with return type int
// I am not sure how to get the values into the function
}
// this is how I want to call the function
int temp = some_function<1,0,0,0>();
对函数声明有什么建议吗?
对于您的用例,您可以使用递归来完成您想要的事情。为此,您需要两个重载。一个只有一个布尔参数,另一个带有两个布尔参数加上可变参数部分。这样您就可以在遍历参数包的过程中分别访问每个值。在这种情况下,
// quick and dirty pow fucntion. There are better ones out there like https://stackoverflow.com/a/101613/4342498
template <typename T, typename U>
auto pow(T base, U exp)
{
T ret = 1;
for (int i = 0; i < exp; ++i)
ret *= base;
return ret;
}
template <bool Last>
int some_function()
{
return Last;
}
template <bool First, bool Second, bool... Rest>
int some_function()
{
return First * pow(2, sizeof...(Rest) + 1) + some_function<Second, Rest...>();
}
int main()
{
std::cout << some_function<1,0,0,0>();
}
哪个输出:
8
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句