unique_ptr< T>用于数组特化的lambda自定义删除程序 [英] unique_ptr<T> lambda custom deleter for array specialization
问题描述
这是我的第一篇论坛,所以如果我犯了一些格式错误,请原谅我。我最近开始移植大量的我现有的C ++应用程序代码到C ++ 11,现在我转换到新的智能指针 std :: unique_ptr 和 std :: shared_ptr <强>,我有一个关于自定义删除程序的具体问题。我想添加一个lambda记录器来查看我的删除被调用,但我不能得到数组专业化版本编译。
This is my first post to the forum so forgive me if I make a couple of formatting mistakes. I recently started porting lots of my existing C++ application code to over to C++11 and now that I am converting to the new smart pointers std::unique_ptr and std::shared_ptr, I have a specific question about custom deleters. I want to add a lambda logger to see where my deletes are being called but I cannot get the array specialization version to compile. Advice would be very much appreciated.
我一直在寻找一个用于数组特化的自定义删除器的例子 unique_ptr ,用于 VC ++ 10 或 GCC 4.5.2 + 。我想打印一个日志消息,当删除者在lambda调用 - 主要是为了确保所有的指针,我认为是超出范围是这样做。这是可能的专业化的阵列版本吗?我可以得到它使用非数组版本,如果我传递一个外部结构MyArrayDeleter作为第二个参数,我也可以得到它与数组专业化。还有一件事,是否可以删除丑陋的 std :: function ,因为我认为我可以让lambda签名图。
I have been searching in vain for an example of a custom deleter for array specialization unique_ptr for VC++10 or GCC 4.5.2+. I would like to print a log message when the deleters are called in a lambda - mainly to make sure that all the pointers that I think are going out of scope are doing so. Is this possible for the array version of the specialization? I can get it to work with the non array version, and I can also get it to work with an array specialization if I pass an external struct "MyArrayDeleter" as the second argument. One more thing, would it be possible to remove the ugly std::function as I thought that I could let the lambda signature figure that out.
任何帮助或建议将非常感谢。
感谢
John
Any help or advice would be greatly appreciated. Thanks John
struct MySimpleDeleter {
void operator()(int* ptr) const {
printf("Deleting int pointer!\n");
delete ptr;
}
};
struct MyArrayDeleter {
void operator()(int* ptr) const {
printf("Deleting Array[]!\n");
delete [] ptr;
}
};
{
// example 1 - calls MySimpleDeleter where delete simple pointer is called
std::unique_ptr<int, MySimpleDeleter> ptr1(new int(5));
// example 2 - correctly calls MyArrayDeleter where delete[] is called
std::unique_ptr<int[], MyArrayDeleter> ptr2(new int[5]);
// example 3 - this works (but default_delete<int[]> would have been passed
// even if I did not specialize it as it is the default second arg
// I only show it here to highlight the problem I am trying to solve
std::unique_ptr<int[], std::default_delete<int[]>> ptr2(new int[100]);
// example 3 - this lambda is called correctly - I want to do this for arrays
std::unique_ptr<int, std::function<void (int *)>> ptr3(
new int(3), [&](int *ptr){
delete ptr; std::cout << "delete int* called" << std::endl;
});
// example 4 - I cannot get the following like to compile
// PLEASE HELP HERE - I cannot get this to compile
std::unique_ptr<int[], std::function<void (int *)>> ptr4(
new int[4], [&](int *ptr){
delete []ptr; std::cout << "delete [] called" << std::endl;
});
}
The compiler error is as follows:
The error from the compiler (which complains about the new int[4] for ptr4 below is:
'std::unique_ptr<_Ty,_Dx>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty,_Dx>'
1> with
1> [
1> _Ty=int [],
1> _Dx=std::tr1::function<void (int *)>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2513) : see declaration of 'std::unique_ptr<_Ty,_Dx>::unique_ptr'
1> with
1> [
1> _Ty=int [],
1> _Dx=std::tr1::function<void (int *)>
1> ]
推荐答案
关于:
auto deleter=[&](int* ptr){...};
std::unique_ptr<int[], decltype(deleter)> ptr4(new int[4], deleter);
这篇关于unique_ptr< T>用于数组特化的lambda自定义删除程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!