调用unique_ptr子类的继承模板构造函数 [英] Calling inherited template constructor of unique_ptr subclass
问题描述
这不是模板构造函数或调用继承的模板构造函数的问题的重复。
它具体涉及在类的子类中调用继承的构造函数
问题
为了使代码更容易理解,我在此示例中使用使用
:
using B = std :: unique_ptr< int *,decltype(& :: free)>
class int_ptr:public B {
int_ptr(int * b):B(b,& :: free){};
};
但编译失败:
在构造函数'int_ptr :: int_ptr(int *)':
错误:没有匹配的函数调用
'std :: unique_ptr< int *,void void *)throw()> :: unique_ptr(int *& void(*)(void *)throw())'
int_ptr(int * b):B ;::自由) { };
^
缺少功能匹配的唯一可能的原因可以想到的是存在的 throw()
但我不知道该怎么做,或者如果它甚至是一个问题。可能 unique_ptr
是不抛弃。
否则不匹配函数正是我期望匹配
原因
我不想每当我声明一个unique_ptr的实例时,保持指定析构函数。在此答案中有一个很好的选择 http://stackoverflow.com/a/16615285/2332068 ,但我有兴趣知道我的尝试是什么。
当然,在现实生活中它不是一个 int *
我试图换行在 unique_ptr
,但一些不透明的(但不是 void *
)指针。
我的目的是让所有这些指针从一个C API正确释放,当作用域退出。
也许我可以约束这些
结果
结果 p>从@RSahu的提示,我已经想出了这个,
unique_dptr
,以避免需要继续指定析构函数,但作为替代覆盖模板删除功能在std名称空间中: void free_int(int * p){
delete p;
}
template< typename T,void(* D)(T *)>
class unique_dptr:public std :: unique_ptr< T,decltype(D)> {
public:unique_dptr(T * t):std :: unique_ptr< T,decltype(D)>(t,D){};
};
使用int_ptr = unique_dptr< int,:: free_int> ;;
int_ptr i(new int(2));
std :: unique< int>
意味着 int
的智能指针,即替换 int *
。
当使用 std :: unique< int *>
时,指针需要 int **
。
而不是
using B = std :: unique_ptr< int *,decltype(& :: free)>
使用
using B = std :: unique_ptr< int,decltype(& :: free)>
工作代码(感谢@CompuChip): http://ideone.com/ul29vr 。
This is not a duplicate of questions on template constructors or even on calling inherited template constructors.
It is specifically about calling the inherited constructor in a subclass of an class instance(?) of the unique_ptr<...,...> template.
The problem
To make the code easier to understand, I'm using using
in this example:
using B = std::unique_ptr<int *, decltype(&::free)>;
class int_ptr : public B {
int_ptr(int *b) : B(b, &::free) { };
};
but compilation fails:
In constructor 'int_ptr::int_ptr(int*)':
error: no matching function for call to
'std::unique_ptr<int*, void (*)(void*) throw ()>::unique_ptr(int*&, void (*)(void*) throw ())'
int_ptr(int *b) : B(b, &::free) { };
^
The only possible cause of lack of function matching I can think of is the presence of throw ()
but I'm not sure what to do about that, or if it even is a problem. Possibly unique_ptr
is no-throw.
Otherwise the no-matching function is precisely what I would expect to have matched.
The reason
I don't want to keep specifying the destructor every time I declare an instance of a unique_ptr. There is a good alternative in this answer http://stackoverflow.com/a/16615285/2332068, but I'm interested to know what is wrong with my attempt.
Of course, in real life it is not an int*
I'm trying to wrap in unique_ptr
, but some opaque (but not void*
) pointer.
My intent is to have all these pointers from a C API correctly freed when the scope exits.
Perhaps I could constrain these pointers into a class/struct with a destructor but I can't see that it would save much.
Result
With the tip from @RSahu, I have come up with this, unique_dptr
to avoid the need to keep specifying the destructor, but as an alternative to overriding the template delete function in the std namespace:
void free_int(int* p) {
delete p;
}
template<typename T, void (*D)(T*)>
class unique_dptr : public std::unique_ptr<T, decltype(D)> {
public: unique_dptr(T* t) : std::unique_ptr<T, decltype(D)>(t, D) { };
};
using int_ptr = unique_dptr<int, ::free_int>;
int_ptr i(new int(2));
std::unique<int>
is meant to be smart pointer for int
s, i.e. a replacement for int*
.
When you use std::unique<int*>
, the pointer needs to be int**
.
Instead of
using B = std::unique_ptr<int *, decltype(&::free)>;
use
using B = std::unique_ptr<int, decltype(&::free)>;
Working code (Thanks, @CompuChip): http://ideone.com/ul29vr.
这篇关于调用unique_ptr子类的继承模板构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!