I wonder how I can have a specialized template for when my type has a specific method. Let's take the following code as an example:
template<typename T, typename... Args>
void foo(T&& arg, Args&&... args)
{
std::cout << "called 1\n";
}
template<typename T, std::enable_if_t<std::is_member_function_pointer_v<decltype(&T::log)>, int> = 0>
void foo(T&& v)
{
v.log();
}
struct Y
{
void log() const
{
std::cout << "called 2\n";
}
};
int main()
{
foo(1); // <-- print "called 1";
Y y;
foo(y); // <-- print "called 2";
}
I should say that this code does not work (2nd foo
will use main template and prints called 1
) and I know I cannot partially specialize a function call, but it shows what I need.
Any idea how I can achieve something like this? Should I use structures to partially specialize and then use helper functions?