S1lentSt0rm S1lentSt0rm - 1 year ago 74
C++ Question

unique_ptr constructor with custom deleter is deleted

This example compiles and runs well with gcc 4.8.3:

#include <memory>
#include <functional>
#include <iostream>

int main() {
auto str = new const char[6]{'h', 'e', 'l', 'l', 'o', '\0'};
std::unique_ptr<const char[], std::function<void(const char *)>> u_ptr(str, [](const char *s){ delete[] s; });
std::cout << u_ptr.get() << std::endl;

But when I try it with Visual Studio Professional 2013 it doesn't compile (complains about a deleted function).
Is this not yet possible with Visual Studio 2013? Or is my sample code wrong and gcc ignores my mistake?

Error is:

main.cpp(8) : error C2280: 'std::unique_ptr>::unique_ptr>(_Ptr2,_Dx2)' :
attempting to reference a deleted function
_Ptr2=const char * , _Dx2=main::
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\memory(16 16) : see declaration of 'std::unique_ptr>::unique_ptr'

Answer Source

This appears to be a defect in the Visual C++ 2013 standard library. I cannot reproduce the problem on 2015.

The unique_ptr class has this constructor for taking a pointer and a deleter:

unique_ptr(pointer _Ptr,
    typename _If<is_reference<_Dx>::value, _Dx,
        const typename remove_reference<_Dx>::type&>::type _Dt) _NOEXCEPT
    : _Mybase(_Ptr, _Dt)
    {   // construct with pointer and (maybe const) deleter&

However, the unique_ptr<T[]> specialization also has a catch-all constructor:

template<class _Ptr2,
    class _Dx2>
    unique_ptr(_Ptr2, _Dx2) = delete;

This version is preferred over the previous one.

However, because the non-specialized unique_ptr doesn't have it at all, changing u_ptr to a const char instead of const char[] fixes the problem.

Using the array version with a deleter like you're doing is also unnecessary:

  1. If you want to call delete[] on your pointer, there's already a specialization for arrays. You don't need a custom deleter.

  2. If you want to do something else, you should use the non-specialized version.