Skip to content

Instantly share code, notes, and snippets.

@wackoisgod
Created September 11, 2014 06:59
Show Gist options
  • Save wackoisgod/93a903a87a00082e1834 to your computer and use it in GitHub Desktop.
Save wackoisgod/93a903a87a00082e1834 to your computer and use it in GitHub Desktop.
#include "stdafx.h"
class PrivateClass{
private:
void SomeFunction() { printf("Hello World"); };
};
template <class Tag>
struct touch
{
static typename Tag::type value;
};
template <class Tag>
typename Tag::type touch<Tag>::value;
template <class Tag, typename Tag::type x>
struct touch_private
{
touch_private() { touch<Tag>::value = x; }
static touch_private instance;
};
template <class Tag, typename Tag::type x>
touch_private<Tag, x> touch_private<Tag, x>::instance;
struct AS { typedef void(PrivateClass::*type)(); };
template struct touch_private < AS, &PrivateClass::SomeFunction >;
int _tmain(int argc, _TCHAR* argv[])
{
PrivateClass c;
(c.*touch<AS>::value)();
return 0;
}
@jepio
Copy link

jepio commented Sep 11, 2014

Let me get this correctly:

  1. On line 27 you call for the compiler to create the template struct touch_private.
  2. The static member touch_private::instance gets created on line 24.
  3. In the constructor of this static member the function PrivateClass::SomeFunction gets assigned to the static member touch<AS>::value which is of type pointer to member of PrivateClass that takes no parameters and returns void.
  4. Which then gets called in main.

Does this all have any practical application or is it just for fun? 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment