fork download
  1. #include <iostream>
  2.  
  3.  
  4. // The essence
  5.  
  6. namespace essence {
  7. template<class Ptr, size_t Id>
  8. Ptr result;
  9.  
  10. template<class Ptr, Ptr TargetPointer, size_t InstantiationId>
  11. struct steal {
  12. struct exec_on_instantiation {
  13. exec_on_instantiation() {
  14. result<Ptr, InstantiationId> = TargetPointer;
  15. }
  16. };
  17. static exec_on_instantiation essence;
  18. };
  19. template <typename Ptr, Ptr TargetPointer, size_t InstantiationId>
  20. typename steal<
  21. Ptr,
  22. TargetPointer,
  23. InstantiationId
  24. >::exec_on_instantiation steal<
  25. Ptr,
  26. TargetPointer,
  27. InstantiationId
  28. >::essence;
  29. } // namespace essence
  30.  
  31.  
  32. // Demo
  33.  
  34. using std::string;
  35. using std::cout;
  36. using std::endl;
  37.  
  38. class Victim {
  39. void f() { cout << "PoC - member function f" << endl; }
  40. void h() { cout << "PoC - member function h" << endl; }
  41. string str = "PoC - member data";
  42. static void g() { cout << "PoC - static member function" << endl; }
  43. static string static_str;
  44. };
  45. string Victim::static_str = "PoC - static member data";
  46.  
  47. template struct essence::steal<decltype(&Victim::f), &Victim::f, 1>;
  48. template struct essence::steal<decltype(&Victim::h), &Victim::h, 2>;
  49. template struct essence::steal<decltype(&Victim::str), &Victim::str, 3>;
  50. template struct essence::steal<decltype(&Victim::g), &Victim::g, 4>;
  51. template struct essence::steal<
  52. decltype(&Victim::static_str),
  53. &Victim::static_str,
  54. 5
  55. >;
  56.  
  57. int main() {
  58. auto ptr1 = essence::result<void (Victim::*)(), 1>;
  59. auto ptr2 = essence::result<void (Victim::*)(), 2>;
  60. auto ptr3 = essence::result<string (Victim::*), 3>;
  61. auto ptr4 = essence::result<void(*)(), 4>;
  62. auto ptr5 = essence::result<string*, 5>;
  63.  
  64. Victim a;
  65.  
  66. (a.*ptr1)();
  67. (a.*ptr2)();
  68. a.*ptr3 += " (modified)", cout << a.*ptr3 << endl;
  69. ptr4();
  70. *ptr5 += " (modified)", cout << *ptr5 << endl;
  71. }
Success #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
PoC - member function f
PoC - member function h
PoC - member data (modified)
PoC - static member function
PoC - static member data (modified)