Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <memory>
  4. #include <typeinfo>
  5. #include <vector>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10. template <typename T>
  11. struct leaking {
  12. union {
  13. T data;
  14. };
  15. leaking() {
  16. new(&data) T();
  17. }
  18. template <typename... A>
  19. leaking(A&&... args) {
  20. new(&data) T(std::forward<A>(args)...);
  21. }
  22. template <typename U>
  23. leaking(std::initializer_list<U> inli) {
  24. new(&data) T(inli);
  25. }
  26. ~leaking() {
  27. // leak
  28. }
  29. T& get() {
  30. return data;
  31. }
  32. explicit operator T&() {
  33. return data;
  34. }
  35. auto operator ->() {
  36. return &data;
  37. }
  38. };
  39.  
  40. struct hello {
  41. static int dtor;
  42. ~hello() {
  43. dtor += 1;
  44. }
  45. };
  46. int hello::dtor = 0;
  47.  
  48. int main() {
  49. {
  50. leaking<hello> vec;
  51. }
  52.  
  53. assert(hello::dtor == 0);
  54.  
  55. {
  56. hello vec;
  57. }
  58.  
  59. assert(hello::dtor == 1);
  60.  
  61. system("pause");
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement