#include <iostream>
#include <memory>
struct releaseable_object
{
releaseable_object() : dummy_ptr(new int) {}
void Release()
{
std::cout << "Releasing releaseable object\n";
delete dummy_ptr;
}
int *const dummy_ptr;
};
struct A : releaseable_object {};
struct B : releaseable_object {};
namespace factory
{
A *CreateA(/* lots of parameters */) { return new A; }
B *CreateB(/* lots of parameters */) { return new B; }
}
template <typename T> struct Managed
{
using type = T;
static void deleter(type *object)
{
std::cout << "Release!\n";
object->Release();
};
using pointer = std::unique_ptr<T, decltype(deleter)>;
};
struct ManagedA : Managed<A>
{
using base = Managed<A>;
using base::pointer;
using base::deleter;
ManagedA(/* lots of parameters */) :
m_pointer(nullptr, deleter)
{
// do A specific stuff...
A *a = factory::CreateA(/* lots of parameters */);
// more A specific stuff...
// wrap the pointer:
m_pointer.reset(a);
}
pointer m_pointer;
};
int main()
{
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKc3RydWN0IHJlbGVhc2VhYmxlX29iamVjdAp7CglyZWxlYXNlYWJsZV9vYmplY3QoKSA6IGR1bW15X3B0cihuZXcgaW50KSB7fQoJdm9pZCBSZWxlYXNlKCkKCXsKCQlzdGQ6OmNvdXQgPDwgIlJlbGVhc2luZyByZWxlYXNlYWJsZSBvYmplY3RcbiI7CgkJZGVsZXRlIGR1bW15X3B0cjsKCX0KCWludCAqY29uc3QgZHVtbXlfcHRyOwp9OwoKc3RydWN0IEEgOiByZWxlYXNlYWJsZV9vYmplY3Qge307CnN0cnVjdCBCIDogcmVsZWFzZWFibGVfb2JqZWN0IHt9OwoKbmFtZXNwYWNlIGZhY3RvcnkKewoJQSAqQ3JlYXRlQSgvKiBsb3RzIG9mIHBhcmFtZXRlcnMgKi8pIHsgcmV0dXJuIG5ldyBBOyB9CglCICpDcmVhdGVCKC8qIGxvdHMgb2YgcGFyYW1ldGVycyAqLykgeyByZXR1cm4gbmV3IEI7IH0KfQoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+IHN0cnVjdCBNYW5hZ2VkCnsKCXVzaW5nIHR5cGUgPSBUOwoJc3RhdGljIHZvaWQgZGVsZXRlcih0eXBlICpvYmplY3QpCgl7CgkJc3RkOjpjb3V0IDw8ICJSZWxlYXNlIVxuIjsKCQlvYmplY3QtPlJlbGVhc2UoKTsKCX07Cgl1c2luZyBwb2ludGVyID0gc3RkOjp1bmlxdWVfcHRyPFQsIGRlY2x0eXBlKGRlbGV0ZXIpPjsKfTsKCnN0cnVjdCBNYW5hZ2VkQSA6IE1hbmFnZWQ8QT4KewoJdXNpbmcgYmFzZSA9IE1hbmFnZWQ8QT47Cgl1c2luZyBiYXNlOjpwb2ludGVyOwoJdXNpbmcgYmFzZTo6ZGVsZXRlcjsKCU1hbmFnZWRBKC8qIGxvdHMgb2YgcGFyYW1ldGVycyAqLykgOgoJCW1fcG9pbnRlcihudWxscHRyLCBkZWxldGVyKQoJewoJCS8vIGRvIEEgc3BlY2lmaWMgc3R1ZmYuLi4KCQlBICphID0gZmFjdG9yeTo6Q3JlYXRlQSgvKiBsb3RzIG9mIHBhcmFtZXRlcnMgKi8pOwoJCS8vIG1vcmUgQSBzcGVjaWZpYyBzdHVmZi4uLgoKCQkvLyB3cmFwIHRoZSBwb2ludGVyOgoJCW1fcG9pbnRlci5yZXNldChhKTsKCX0KCXBvaW50ZXIgbV9wb2ludGVyOwp9OwoKaW50IG1haW4oKQp7CglyZXR1cm4gMDsKfQ==
In file included from /usr/include/c++/4.8/functional:55:0,
from /usr/include/c++/4.8/memory:79,
from prog.cpp:2:
/usr/include/c++/4.8/tuple: In instantiation of ‘struct std::_Head_base<1u, void(A*), false>’:
/usr/include/c++/4.8/tuple:229:12: recursively required from ‘struct std::_Tuple_impl<1u, void(A*)>’
/usr/include/c++/4.8/tuple:229:12: required from ‘struct std::_Tuple_impl<0u, A*, void(A*)>’
/usr/include/c++/4.8/tuple:521:11: required from ‘class std::tuple<A*, void(A*)>’
/usr/include/c++/4.8/bits/unique_ptr.h:127:57: required from ‘class std::unique_ptr<A, void(A*)>’
prog.cpp:50:10: required from here
/usr/include/c++/4.8/tuple:172:13: error: field ‘std::_Head_base<1u, void(A*), false>::_M_head_impl’ invalidly declared function type
_Head _M_head_impl;
^
/usr/include/c++/4.8/tuple: In instantiation of ‘static constexpr _Head& std::_Head_base<_Idx, _Head, false>::_M_head(std::_Head_base<_Idx, _Head, false>&) [with unsigned int _Idx = 1u; _Head = void(A*)]’:
/usr/include/c++/4.8/tuple:239:69: required from ‘static constexpr _Head& std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_head(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with unsigned int _Idx = 1u; _Head = void(A*); _Tail = {}]’
/usr/include/c++/4.8/tuple:743:60: required from ‘constexpr typename std::__add_ref<_Head>::type std::__get_helper(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with unsigned int __i = 1u; _Head = void(A*); _Tail = {}; typename std::__add_ref<_Head>::type = void (&)(A*)]’
/usr/include/c++/4.8/tuple:758:35: required from ‘constexpr typename std::__add_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type std::get(std::tuple<_Elements ...>&) [with unsigned int __i = 1u; _Elements = {A*, void(A*)}; typename std::__add_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type = void (&)(A*)]’
/usr/include/c++/4.8/bits/unique_ptr.h:238:32: required from ‘std::unique_ptr<_Tp, _Dp>::deleter_type& std::unique_ptr<_Tp, _Dp>::get_deleter() [with _Tp = A; _Dp = void(A*); std::unique_ptr<_Tp, _Dp>::deleter_type = void(A*)]’
/usr/include/c++/4.8/bits/unique_ptr.h:184:16: required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = A; _Dp = void(A*)]’
prog.cpp:41:29: required from here
/usr/include/c++/4.8/tuple:167:54: error: invalid initialization of reference of type ‘void (&)(A*)’ from expression of type ‘void (*)(A*)’
_M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
^
/usr/include/c++/4.8/tuple:167:68: error: body of constexpr function ‘static constexpr _Head& std::_Head_base<_Idx, _Head, false>::_M_head(std::_Head_base<_Idx, _Head, false>&) [with unsigned int _Idx = 1u; _Head = void(A*)]’ not a return-statement
_M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
^