I would like to be able to form a pointer-to-member type knowing only the class itself and method name. Unfortunately I am not able to do so having a const and non-const method variants in my class.
Example code snippet:
struct A
{
void method() {}
void method() const {}
};
int main()
{
decltype(&A::method) _;
}
I have also tried the following this but without much success either:
decltype(&(std::declval<const A>().method)) _;
Both approaches fail as decltype cannot resolve this due to ambiguity:
'decltype cannot resolve address of overloaded function'
How can I achieve this in some other way?
You can do it like this:
If you want to use
decltype()to achieve the same thing, you first have to manually select the function you want usingstatic_cast<>:Live on Coliru