Guest User

Untitled

a guest
Nov 21st, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. // Koenig lookup + explicit instantiation = access specifiers ko.
  4. // a modified version of a well known hack (C++standard/sutter/litb/dabrahams/...)
  5. // purpose is to deal with static private member functions and violate access specifiers
  6. // exercise is not about the access specifiers, but about where get() is actually found
  7. // compiles C++0x
  8.  
  9. struct Victim {
  10. private:
  11. static int password()
  12. { return 1234; }
  13. };
  14.  
  15. //
  16. template<typename T, int (*x)()>
  17. struct access_violator {
  18. constexpr friend decltype(x) get(T)
  19. { return x; }
  20. };
  21.  
  22. struct access_proxy {
  23. typedef int (*type)();
  24. constexpr friend type get(access_proxy);
  25. };
  26.  
  27. // explicit instantiation, won't work without it (disregards specifiers)
  28. template struct access_violator<access_proxy, &Victim::password>;
  29.  
  30. int main() {
  31. // why is get() in global namespace ? :)
  32. printf("%d\n", (*get(access_proxy()))());
  33. }
Add Comment
Please, Sign In to add comment