2

I have this code :

// 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);

And it is giving me Errors:

 c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory
  (1418) : see declaration of 'std::tr1::shared_ptr'c:\cef\appjs_final\appjs\src\includes\util.h(27): 
error C2977: 'std::tr1::shared_ptr' : too many template arguments 
 [C:\CEF\appjs_final\appjs\build\appjs.vcxproj]
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95

1 Answers1

7

Unlike with unique_ptr, the deleter is not a class template parameter. The deleter is stored along with the usage count in a separate object, so type erasure can be used to make the pointer object itself agnostic to the deleter type.

There are constructor templates allowing you to initialise the pointer with any suitable functor type. So your functions are simply

std::shared_ptr<char> V8StringToChar(Whatever);

and they will create the pointer with a suitable deleter

return std::shared_ptr<char>(array, ArrayDeleter<char>());
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644