C++ member function overloading with & (ampersand)
Since an answer is already given, I'll present a solution that's easier on the eyes. This situation is perfect for a macro if you don't want to write out the cast every time:
template<class T>
using test_type_t = T test::*;
#define TEST_CAST(T, F) static_cast<test_type_t<T>>(&F)
auto f1 = TEST_CAST(void(int), test::error);
You can just explicitly specify the member function pointer type.
int (test::*f1)() = &test::error;
void (test::*f2)(int) = &test::error;
You'll need to use a static_cast
to disambiguate.
&test::error
is not evaluable since the function is overloaded. The fact that you are assigning this to something marked auto
is not immediately relevant.
One fix would be to use static_cast<int(test::*)()>(&test::error)
or static_cast<void(test::*)(int)>(&test::error)
as appropriate.
Then auto
will work since there will be no ambiguity in the type deduction.
Do I need to cast somehow?
Yes, you can use static_cast
.
static_cast
may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as instd::for_each(files.begin(), files.end(), static_cast<std::ostream&(*)(std::ostream&)>(std::flush));
So you can:
auto f1 = static_cast<int(test::*)()>(&test::error);
auto f2 = static_cast<void(test::*)(int)>(&test::error);