unique_ptr memory compress

1
2
3
4
5
6
7
8
9
10
#include <memory>
#include <iostream>
int main()
{
	auto deleter = [](int* ptr) { delete ptr; };
	std::cout << sizeof(int*) << std::endl;//4
	std::cout << sizeof(std::unique_ptr<int>) << std::endl;//4!!
	std::cout << sizeof(std::unique_ptr<int,decltype(deleter)>) << std::endl;//4!!
	std::cout << sizeof(std::unique_ptr<int,void(*)(int*)>) << std::endl;//8!!
}

how does 'unique_ptr' implement efficient memory usage?
Typically by exploiting empty base optimisation when the deleter is a class type with no non-static members.
http://en.cppreference.com/w/cpp/language/ebo

For instance, the managed pointer and the deleter could be stored in a compressed pair akin to boost::compressed_pair http://www.boost.org/doc/libs/1_63_0/libs/utility/doc/html/compressed_pair.html
Last edited on
thanks!
Topic archived. No new replies allowed.