0

Windows 10, Visual Studio 2019, C++17:

Compile error: cannot convert argument 2 from 'int (__cdecl *)(int)' to '...'

////////////////////////////////////////////////////////////////////
// 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;
}    

Without the template (int func(int size) ) the code compiles ok. How do I modify this to make the compiler understand the variadic argument?

jdaz
  • 5,964
  • 2
  • 22
  • 34

1 Answers1

0

The issue is func is being treated as a function pointer, but pointers to template functions are not allowed in C++.

You need to specify the type you plan to use when you reference func, like:

result = usefunc(0, func<int>);

You can use decltype to reference the type of a variable, to be a little more flexible:

result = usefunc(0, func<decltype(result)>);
jdaz
  • 5,964
  • 2
  • 22
  • 34