-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_ptr_deleter_size_trick.cpp
72 lines (58 loc) · 2.18 KB
/
unique_ptr_deleter_size_trick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <memory>
#include <cassert>
#include <type_traits>
#include "FunctionFunctor.h"
void foo0() {}
void foo1(int) {}
void foo2(int, float) {}
int bar0() { return 0; }
float bar1(int) { return 0; }
double bar2(int, float) { return 0; }
static_assert(std::is_empty<FUNCTION_FUNCTOR(foo0)>::value, "FunctionFunctor not empty !");
static_assert(std::is_empty<FUNCTION_FUNCTOR(foo1)>::value, "FunctionFunctor not empty !");
static_assert(std::is_empty<FUNCTION_FUNCTOR(foo2)>::value, "FunctionFunctor not empty !");
static_assert(std::is_empty<FUNCTION_FUNCTOR(bar0)>::value, "FunctionFunctor not empty !");
static_assert(std::is_empty<FUNCTION_FUNCTOR(bar1)>::value, "FunctionFunctor not empty !");
static_assert(std::is_empty<FUNCTION_FUNCTOR(bar2)>::value, "FunctionFunctor not empty !");
// a make_unique with deleter with a trick to limit size
template< class T, class D, class... Args >
auto make_unique_with_deleter(Args&&... args)
{
return std::unique_ptr<T, D>{ new T(std::forward<Args>(args)...) };
}
template< class T, void f(T*), class... Args >
auto make_unique_with_deleter(Args&&... args)
{
return std::unique_ptr<T, FUNCTION_FUNCTOR(f)>{ new T(std::forward<Args>(args)...) };
}
// test
template <typename T>
bool g_deleterCalled = false;
template <typename T>
void deleter(T * p)
{
g_deleterCalled<T> = true;
delete p;
}
static_assert(std::is_empty<FUNCTION_FUNCTOR(deleter<int>)>::value, "FunctionFunctor not empty !");
int main()
{
{
// The common way
std::unique_ptr<int, void(*)(int*)> p{ new int(), deleter<int> };
static_assert(sizeof(p) == sizeof(int *) + sizeof(void(*)(int*)), "Not the expected size");
}
assert(g_deleterCalled<int>);
{
// templating with the function
auto p = make_unique_with_deleter<float, deleter<float>>();
static_assert(sizeof(p) == sizeof(float *), "No ebo in implementation or ebo failure");
}
assert(g_deleterCalled<float>);
{
// check functors are still ok
auto p_functor = make_unique_with_deleter<int, std::default_delete<int>>();
static_assert(sizeof(p_functor) == sizeof(int *), "No ebo in implementation or ebo failure");
}
return 0;
}