29

Why doesn't this work?

#include <map>
#include <memory>

void deleter(int* i) {
    delete i;
}

std::map<int, std::unique_ptr<int, decltype(&deleter)>> m;

void foo(int* i) {
    m[0] = std::unique_ptr<int, decltype(&deleter)>(i, &deleter);
}

Check out the incomprehensible compile error https://godbolt.org/z/Uhp9NO.

In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/map:61:
In file included from /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_map.h:63:
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:1668:9: error: no matching constructor for initialization of 'std::unique_ptr<int, void (*)(int *)>'
        second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
        ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:1655:9: note: in instantiation of function template specialization 'std::pair<const int, std::unique_ptr<int, void (*)(int *)> >::pair<int &&, 0>' requested here
      : pair(__first, __second,
        ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/ext/new_allocator.h:136:23: note: in instantiation of function template specialization 'std::pair<const int, std::unique_ptr<int, void (*)(int *)> >::pair<int &&>' requested here
        { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                             ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/alloc_traits.h:475:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::construct<std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
        { __a.construct(__p, std::forward<_Args>(__args)...); }
              ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:637:23: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<std::_Rb_tree_node<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > > >::construct<std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
              _Alloc_traits::construct(_M_get_Node_allocator(),
                             ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:654:4: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_construct_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
          _M_construct_node(__tmp, std::forward<_Args>(__args)...);
          ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_tree.h:2414:19: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_create_node<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
        _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
                         ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/stl_map.h:518:15: note: in instantiation of function template specialization 'std::_Rb_tree<int, std::pair<const int, std::unique_ptr<int, void (*)(int *)> >, std::_Select1st<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > >, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::_M_emplace_hint_unique<const std::piecewise_construct_t &, std::tuple<int &&>, std::tuple<> >' requested here
          __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
                     ^
<source>:11:6: note: in instantiation of member function 'std::map<int, std::unique_ptr<int, void (*)(int *)>, std::less<int>, std::allocator<std::pair<const int, std::unique_ptr<int, void (*)(int *)> > > >::operator[]' requested here
    m[0] = std::unique_ptr<int, decltype(&deleter)>(i, &deleter);
     ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:191:12: note: candidate template ignored: substitution failure [with _Up = void (*)(int *)]: no type named 'type' in 'std::enable_if<false, void>'
        constexpr unique_ptr() noexcept
                  ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:204:2: note: candidate constructor template not viable: requires single argument '__p', but no arguments were provided
        unique_ptr(pointer __p) noexcept
        ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:236:12: note: candidate constructor template not viable: requires 1 argument, but 0 were provided
        constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
                  ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:255:2: note: candidate constructor template not viable: requires single argument '__u', but no arguments were provided
        unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
        ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:265:2: note: candidate constructor template not viable: requires single argument '__u', but no arguments were provided
        unique_ptr(auto_ptr<_Up>&& __u) noexcept;
        ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:241:7: note: candidate constructor not viable: requires single argument '__u', but no arguments were provided
      unique_ptr(unique_ptr&& __u) noexcept
      ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:394:7: note: candidate constructor not viable: requires 1 argument, but 0 were provided
      unique_ptr(const unique_ptr&) = delete;
      ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:215:7: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
      unique_ptr(pointer __p,
      ^
/opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/bits/unique_ptr.h:227:7: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
      unique_ptr(pointer __p,
      ^
1 error generated.
Compiler returned: 1
grg
  • 5,023
  • 3
  • 34
  • 50
Timmmm
  • 88,195
  • 71
  • 364
  • 509
  • 6
    I don't know why this is getting downvotes, it's a good question. Perhaps it's the link. People might see it as a link to external code. To be clear to other readers, it's a live sample at godbolt. Edit : I've edited the question to make the nature of the link more obvious. – François Andrieux Oct 05 '18 at 15:47
  • 2
    You need to post errors here. – Maxim Egorushkin Oct 05 '18 at 15:47
  • They are very long, and only a click away. That seem excessive. – Timmmm Oct 05 '18 at 15:48
  • This is relevant: https://stackoverflow.com/questions/17906325/unique-ptr-is-not-getting-init-with-default-deleter – zoska Oct 05 '18 at 15:48
  • 3
    On a mobile it is extremely difficult to view errors from godbolt – Robert Andrzejuk Oct 05 '18 at 16:05
  • @Timmmm It's quite a long error message, yes, but you can trim the message down with indications that it's longer than what you post here. If you just post the single message with `error:` in it, that would be good enough. For understanding long C++ compilation errors, you should usually start with the actual `error:` and skip all the `note:`s – Justin Oct 05 '18 at 17:13
  • 2
    With the errors not in the question it makes the question dependent on godbolt being up. I have edited them in. Timmmm your concern about them being excessive makes sense but StackOverflow does add a scroll bar to long code snippets. If you can chomp it down to only the parts you are confused about please feel free to do so. – Captain Man Oct 05 '18 at 17:17

2 Answers2

32

The problem is that m[0] calls the default constructor of std::unique_ptr<int, decltype(&deleter)>, which is not available because it requires the deleter pointer.

Fix:

struct Deleter {
    void operator()(int* i) { delete i; }
};

std::map<int, std::unique_ptr<int, Deleter>> m;

void foo(int* i) {
    m[0] = std::unique_ptr<int, Deleter>(i);
}

This is also more efficient than std::unique_ptr<int, decltype(&deleter)> because it does not have to store the very same pointer to deleter in each instance of std::unique_ptr. I.e. sizeof(std::unique_ptr<int, Deleter>) < sizeof(std::unique_ptr<int, decltype(&deleter)>).

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 3
    Fun fact: `std::integral_constant` can (non-obviously) fill this role: `template using deleter_fn = std::integral_constant; std::unique_ptr>;` – Justin Oct 05 '18 at 17:06
  • @Justin Nice one. Although `std::is_integral::value` is `false`. So, `integral` in `std::integral_constant` is misleading. – Maxim Egorushkin Oct 05 '18 at 17:08
  • 1
    @Justin also, surrogate calls are spoiling the party somewhat, as [smarter souls](https://chat.stackoverflow.com/rooms/10/loungec) have pointed out to me http://coliru.stacked-crooked.com/a/759ea11362b1d2de – sehe Oct 08 '18 at 12:13
14

std::unique_ptr, when using a custom deleter function like you have, is not default constructable. std::map::operator[] requires that the value type of the map be default constructable. That means as is you cannot use operator [] with this type of map.

In order for std::unique_ptr to be default constructable you need a deleter that satisfies where std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • You could also skip the default constructor entirely using something like `m.insert_or_assign(0, std::unique_ptr(i, &deleter));` - that said, I personally definitely prefer the empty deleter object as in Maxim Egorushkin's answer. – Daniel Schepler Oct 05 '18 at 18:58