LxL*_*LxL 6 c++ templates variadic-templates c++11 c++14
我有一个可变函数,如:
void test(int){}
template<typename T,typename...Args>
void test(int& sum,T v,Args... args)
{
sum+=v;
test(sum,args...);
}
Run Code Online (Sandbox Code Playgroud)
我想将它别名为:
auto sum = test;//error : can not deduce auto from test
int main()
{
int res=0;
test(res,4,7);
std::cout<<res;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过使用std::bind但是它不适用于可变参数函数,因为它需要占位符...
是否可以为可变参数函数添加别名?
你正在尝试的与
void test(int)
{
}
void test(double, int)
{
}
auto a = test;
Run Code Online (Sandbox Code Playgroud)
编译器无法检测您要使用哪个重载。
您可以通过以下方式明确test要分配给哪个a:
auto a = (void(*)(int))test;
Run Code Online (Sandbox Code Playgroud)
如果您想将可变参数模板版本添加到组合中,您可以使用:
template<typename T,typename...Args>
void test(int& sum,T v,Args... args)
{
sum+=v;
test(sum,args...);
}
auto a = test<int, int, int>;
Run Code Online (Sandbox Code Playgroud)