调用可变参数模板函数而无args失败 [英] Calling variadic template function with no args failing
本文介绍了调用可变参数模板函数而无args失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下代码:
#include <iostream>
template <typename... Args>
void foo(Args&&... bargs, Args&&... aargs)
{
std::cout << "Hello" << std::endl;
}
int main()
{
foo<int, double>(1, 2.0, 3, 4.0); //OK
foo<char>('c', 'd'); // OK
foo(); //FAIL
}
我得到以下编译器错误:
I get the following compiler error:
In function 'int main()':
15:9: error: no matching function for call to 'foo()'
15:9: note: candidate is:
6:6: note: template<class ... Args> void foo(Args&& ..., Args&& ...)
6:6: note: template argument deduction/substitution failed:
15:9: note: candidate expects 1 argument, 0 provided
是否可以在不使用args的情况下调用该函数?可以将函数更改为支持零个或多个args吗?
Is it possible to call such a function with no args? Can the function be changed to support zero or more args?
推荐答案
您必须指定不带args的函数版本:
You have to specify a version of the function without args:
#include <iostream>
template <typename... Args>
void foo(Args&&... bargs, Args&&... aargs)
{
std::cout << "Hello with args" << std::endl;
}
void foo()
{
std::cout << "Hello without args" << std::endl;
}
int main()
{
foo<int, double>(1, 2.0, 3, 4.0); //OK
foo<char>('c', 'd'); // OK
foo(); // Also OK now
}
这篇关于调用可变参数模板函数而无args失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文