Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Last active December 16, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/5416422 to your computer and use it in GitHub Desktop.
Save chadaustin/5416422 to your computer and use it in GitHub Desktop.
/*
* I am building a mechanism for selecting member function overloads without
* having to use C-style casts. It works today on non-member functions and
* non-const member functions. The syntax looks like:
*
* select_overload<int(int)>(&some_overloaded_function)
* select_overload<int(int)>(&some_overloaded_member_function)
*
* You can see the implementation of select_overload at http://pastie.org/7649267
*
* My question is: how do I match const member functions? I cannot find the
* correct incantations. How can I get either #1 or #2, below, to compile?
*/
#include <stdio.h>
#include <typeinfo>
struct S {
S(): field(0) {}
int field;
void foo() {}
void bar() const {}
};
template<typename Signature, typename ClassType>
void siggy(const char* name, Signature ClassType::*f) {
printf("%s: %s\n", name, typeid(Signature).name());
}
template<typename Signature, typename ClassType>
void const_siggy(const char* name, /* what do i put here? */ Signature ClassType::* f) {
printf("%s: %s\n", name, typeid(Signature).name());
}
int main(int argc, const char* argv[]) {
// prints 'int'
siggy("S::field", &S::field);
// prints 'void __thiscall(void)'
siggy("S::foo", &S::foo);
// #1 - does not match:
// error C2664: 'siggy' : cannot convert parameter 2 from 'void (__thiscall S::* )(void) const' to 'void (__thiscall S::* )(void)'
//siggy("S::bar", &S::bar);
// #2 - does not match:
// error C2664: 'const_siggy' : cannot convert parameter 2 from 'void (__thiscall S::* )(void) const' to 'void (__thiscall S::* )(void)'
//const_siggy("S::bar", &S::bar);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment