Windows 10、Visual Studio 2019、C++17:
编译错误:无法将参数 2 从 'int (__cdecl *)(int)' 转换为 '...'
////////////////////////////////////////////////////////////////////
// This is the templated function that becomes a variadic argument
//
template <typename siz>
int func(siz size)
{
// ...
// ...
return 0;
}
///////////////////////////////////////////////////////////
// This is the function that uses variadic arguments
//
int usefunc(int option, ...)
{
// ...
// ...
return 0;
}
int main()
{
int result;
result = usefunc(0, func); // ** int usefunc(int,...)': cannot convert argument 2 from 'int (__cdecl *)(int)' to '...' **
// Context does not allow for disambiguation of overloaded function
return result;
}
没有模板 (int func(int size) ) 代码编译正常。如何修改它以使编译器理解可变参数?
该问题func
被视为函数指针,但C++ 中不允许指向模板函数的指针。
您需要指定您在引用时计划使用的类型func
,例如:
result = usefunc(0, func<int>);
您可以使用decltype
来引用变量的类型,以更加灵活:
result = usefunc(0, func<decltype(result)>);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句