I have some function templates with same signature, and I'm trying to create a proxy function to call them with different types.
How to pass the function name to the proxy function correctly?
https://godbolt.org/z/4vnos3hEf
template <typename T>
T function()
{
return T();
}
template<template<typename> typename func>
void proxy1(int x)
{
if (x == 0)
func<bool>();
else if (x == 1)
func<int>();
}
void proxy(int x)
{
if (x == 0)
function<bool>();
else if (x == 1)
function<int>();
}
int main()
{
proxy1<function>(0);
proxy(0);
}
With the proxy()
function, there is no problem, but I want to use proxy1()
which takes a function as a template parameter.
Compiler complains about:
<source>(26): error C2672: 'proxy1': no matching overloaded function found"
As per Patrick's comment, you need to make your callable object a class template with an operator ()
member function, something like:
template <typename T> struct call_me
{
T operator () () { return T (); }
};
template<template<typename> typename callable>
void proxy1(int x)
{
if (x == 0)
callable<bool>{}();
else if (x == 1)
callable<int>{}();
}
void proxy(int x)
{
if (x == 0)
call_me<bool>{}();
else if (x == 1)
call_me<int>{}();
}
int main()
{
proxy1<call_me>(0);
proxy(0);
}