Let's consider two functions with same names:
int func(int&)
{
return 7;
}
int func(const int&)
{
return 5;
}
Let int mutableValue = 5
be defined somewhere. Is there any possibility that template function call
in call(mutableValue, func)
would take only int func(int&)
?
I don't want to use static_cast<int(&)(int&)>
- because it's so noisy.
Naive implementation:
template<typename Arg, typename Ret>
void call(Arg& val, Ret (&fun)(Arg&))
{
fun(val);
}
works on gcc, but does not work on clang - solutions must work on both compilers.
Accepting only const Arg&
is easy. Deleting const Arg&
overload does not help.
template<typename Arg, typename Callable> void call(Arg& val, Callable fun) { fun(val); }
instead – Documentationfunc(int&)
andfunc(const int&)
match toCallable
... – OecologyRet
to be deduced, ordecay_t<Arg>(&fun)(Arg&)
ok? – FulgurationRet
s are different fromArg
s. It's just coincidence that they are same infunc
case. – Oecologytemplate<typename Arg, typename Callable> void call(Arg& val, Callable fun) { fun(val); }
andcall(mutableValue, [](auto& arg){ return func(arg); })
... – Fulguration