Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created September 26, 2016 10:11
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 Garciat/dc79201aa230330d3606f5ff499b5ee4 to your computer and use it in GitHub Desktop.
Save Garciat/dc79201aa230330d3606f5ff499b5ee4 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <memory>
#include <typeinfo>
#include <vector>
#include <cassert>
using namespace std;
template <typename T>
struct leaking {
union {
T data;
};
leaking() {
new(&data) T();
}
template <typename... A>
leaking(A&&... args) {
new(&data) T(std::forward<A>(args)...);
}
template <typename U>
leaking(std::initializer_list<U> inli) {
new(&data) T(inli);
}
~leaking() {
// leak
}
T& get() {
return data;
}
explicit operator T&() {
return data;
}
auto operator ->() {
return &data;
}
};
struct hello {
static int dtor;
~hello() {
dtor += 1;
}
};
int hello::dtor = 0;
int main() {
{
leaking<hello> vec;
}
assert(hello::dtor == 0);
{
hello vec;
}
assert(hello::dtor == 1);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment