草庐IT

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - unique_ptr 与 shared_ptr 中的删除器类型

这个问题在这里已经有了答案:Whydoesunique_ptrtaketwotemplateparameterswhenshared_ptronlytakesone?(2个回答)关闭7年前。当我发现标准以两种完全不同的方式定义了std::unique_ptr和std::shared_ptr时,我觉得这非常奇怪可能拥有。这是来自cppreference::unique_ptr的声明和cppreference::shared_ptr:template>classunique_ptr;templateclassshared_ptr;如您所见,unique_ptr将Deleter对象的类型“保

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 你能分配一个与 make_shared 等效的数组吗?

buffer=newchar[64];buffer=std::make_shared(char[64]);???你能用make_shared()为数组分配内存吗??我可以:buffer=std::make_shared(newchar[64]);但这仍然涉及调用new,据我了解make_shared更安全、更高效。 最佳答案 您需要共享分配的内存吗?您可以改用std::unique_ptr和std::make_unique在C++14上可用:autobuffer=std::make_unique(64);会有一个std::make_

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

c++ - 将带有自定义删除器的 unique_ptr 移动到 shared_ptr

我有一个函数可以创建一个带有自定义删除器的unique_ptr并返回它:autogive_unique_ptr(){autodeleter=[](int*pi){deletepi;};int*i=newint{1234};returnstd::unique_ptr(i,deleter);}在该函数的客户端代码中,我想将unique_ptr移动到shared_ptr中,但鉴于我不知道该怎么做在函数之外不知道我的自定义删除器的decltype。我猜它应该是这样的:autouniquePtr=give_unique_ptr();autosharedPtr=std::shared_ptr(st

C++ 编译器允许循环定义?

在为树编写一些代码时出错,我遇到了以下奇怪情况。我已经对这个例子做了很多剥离,所以它只是一个线性树。基本上,在main()函数中,我想将一个节点附加到我的树上,但我没有将它附加到“tree.root”,而是将它附加到“root”。然而,令我惊讶的是,它不仅编译得很好,而且我能够在节点上调用方法。只有当我尝试访问“value”成员变量时才会出错。我想我的主要问题是,为什么编译器没有捕捉到这个错误?std::shared_ptrroot=tree.AddLeaf(12,root);由于RHS上的“根”是一个完全未声明的变量。另外,出于好奇,如果编译器允许它们通过,循环定义是否有实际用例?以

C++ 编译器允许循环定义?

在为树编写一些代码时出错,我遇到了以下奇怪情况。我已经对这个例子做了很多剥离,所以它只是一个线性树。基本上,在main()函数中,我想将一个节点附加到我的树上,但我没有将它附加到“tree.root”,而是将它附加到“root”。然而,令我惊讶的是,它不仅编译得很好,而且我能够在节点上调用方法。只有当我尝试访问“value”成员变量时才会出错。我想我的主要问题是,为什么编译器没有捕捉到这个错误?std::shared_ptrroot=tree.AddLeaf(12,root);由于RHS上的“根”是一个完全未声明的变量。另外,出于好奇,如果编译器允许它们通过,循环定义是否有实际用例?以

c++ - shared_ptr 与非指针资源

在C++11中是否可以使用shared_ptr来控制非指针资源?可以使用unique_ptr来管理非指针资源。这是通过实现一个自定义删除器类来完成的,该类提供:一个typedef{TYPE}指针;其中{TYPE}是非指针资源类型operator()(pointer)释放受控资源...然后使用自定义删除器作为第二个模板参数实例化一个unique_ptr。例如,在Windows下,可以创建一个unique_ptr来管理servicecontrolhandle.这个句柄类型不是通过调用delete来释放的,而是通过调用CloseServiceHandle()来释放的。.这是执行此操作的示例代

c++ - shared_ptr 与非指针资源

在C++11中是否可以使用shared_ptr来控制非指针资源?可以使用unique_ptr来管理非指针资源。这是通过实现一个自定义删除器类来完成的,该类提供:一个typedef{TYPE}指针;其中{TYPE}是非指针资源类型operator()(pointer)释放受控资源...然后使用自定义删除器作为第二个模板参数实例化一个unique_ptr。例如,在Windows下,可以创建一个unique_ptr来管理servicecontrolhandle.这个句柄类型不是通过调用delete来释放的,而是通过调用CloseServiceHandle()来释放的。.这是执行此操作的示例代