std::shared_ptr传递删除器
std::shared_ptr passing the deletor
我有这段代码:
// util.h
#include <memory>
template <class T>
class ArrayDeleter {
public:
void operator () (T* d) const
{ delete [] d; }
};
std::shared_ptr<char, ArrayDeleter<char> > V8StringToChar(v8::Handle<v8::String> str);
std::shared_ptr<char, ArrayDeleter<char> > V8StringToChar(v8::Local<v8::Value> val);
它给了我错误:
c:Program Files (x86)Microsoft Visual Studio 10.0VCincludememory
(1418) : see declaration of 'std::tr1::shared_ptr'c:cefappjs_finalappjssrcincludesutil.h(27):
error C2977: 'std::tr1::shared_ptr' : too many template arguments
[C:CEFappjs_finalappjsbuildappjs.vcxproj]
与unique_ptr
不同,删除器不是类模板参数。删除程序与使用计数一起存储在单独的对象中,因此可以使用类型擦除使指针对象本身与删除程序类型无关。
有一些构造函数模板允许您使用任何合适的函子类型初始化指针。所以你的函数很简单
std::shared_ptr<char> V8StringToChar(Whatever);
他们将使用合适的删除器创建指针
return std::shared_ptr<char>(array, ArrayDeleter<char>());
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 删除[] 的行为 (ptr, 0)
- 唯一/共享 ptr 用于在数组超出范围后自动删除阵列
- 如何从集合中删除共享的 PTR 元素
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误
- 删除PTR C 后会发生什么
- 如何删除带有 int ptr 指向它的数组
- 数据突然从 Boost shared_array/ptr 中删除
- c++中新建和删除ptr包装器类
- 删除ptr时堆损坏
- openv Ptr类.指针对象在调用函数后被删除
- 删除从函数返回的唯一ptr
- 当可以在类对象超出范围之前删除此数据时,类允许访问其数据(通过 ptr/it)是否设计不好