8

我们可以将 lambda 用作带有 std::unique_ptr 的删除器吗?实际上,我是用 clang++ 做到的,而且很高兴这样做。

std::swap用来交换到std::unique_ptr<ObjType, decltyp(deleter)>;where auto deleter = [](struct addrinfo* ptr){if (ptr != nullptr) {freeaddrinfo(ptr);} };。Clang 的交换似乎不需要复制赋值运算符,但 gcc 的 std::swap 需要,正如您在这些日志中看到的那样:

In file included from /usr/include/c++/4.8.1/memory:81:0,
                 from /home/zenol/proj/src/PROJ/TCPClient.cpp:28:
/usr/include/c++/4.8.1/bits/unique_ptr.h: In instantiation of ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = addrinfo; _Dp = Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0]’:
/usr/include/c++/4.8.1/bits/move.h:176:11:   required from ‘void std::swap(_Tp&, _Tp&) [with _Tp = std::unique_ptr<addrinfo, Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0>]’
/home/zenol/proj/src/Proj/SocketHelp.hpp:109:50:   required from ‘void Proj::retrieve_addresses(std::string, int, addrinfo&, addrinfo*&, T&, U) [with T = std::unique_ptr<addrinfo, Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0>; U = Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0; std::string = std::basic_string<char>]’
/home/zenol/proj/src/PROJ/TCPClient.cpp:65:49:   required from here
/usr/include/c++/4.8.1/bits/unique_ptr.h:193:16: erreur: use of deleted function ‘Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0& Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0::operator=(const Proj::TCPClient::connect(const Proj::SocketAddress&, int)::__lambda0&)’
  get_deleter() = std::forward<deleter_type>(__u.get_deleter());
                ^
/home/zenol/proj/src/Proj/TCPClient.cpp:56:21: note: a lambda closure type has a deleted copy assignment operator
     auto deleter = [](struct addrinfo* ptr)
                     ^

标准怎么说?我可以设法交换这两个 std::unique_ptr 吗?它们是一种解决方法吗?(也许将 lambda 封装在 std::function 中?...)

编辑:这是一个应该或多或少相同的小例子:

auto deleter = [](struct addrinfo* ptr)
{if (ptr != nullptr) {freeaddrinfo(ptr);} };

std::unique_ptr<struct addrinfo, decltype(deleter)>
resources_keeper(nullptr, deleter);

int main()
{
    decltype(resources_keeper) plouf1(nullptr, deleter);
    decltype(resources_keeper) plouf2(nullptr, deleter);

    std::swap(plouf1, plouf2);
    return 0;
}

错误 :

In file included from /usr/include/c++/4.8.1/bits/stl_pair.h:59:0,
                 from /usr/include/c++/4.8.1/bits/stl_algobase.h:64,
                 from /usr/include/c++/4.8.1/memory:62,
                 from mini.cpp:1:
/usr/include/c++/4.8.1/bits/move.h: In instantiation of ‘void std::swap(_Tp&, _Tp&) [with _Tp = __lambda0]’:
/usr/include/c++/4.8.1/tuple:381:36:   required from ‘void std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_swap(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 1ul; _Head = __lambda0; _Tail = {}]’
/usr/include/c++/4.8.1/tuple:382:35:   required from ‘void std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_swap(std::_Tuple_impl<_Idx, _Head, _Tail ...>&) [with long unsigned int _Idx = 0ul; _Head = addrinfo*; _Tail = {__lambda0}]’
/usr/include/c++/4.8.1/tuple:667:33:   required from ‘void std::tuple<_T1, _T2>::swap(std::tuple<_T1, _T2>&) [with _T1 = addrinfo*; _T2 = __lambda0]’
/usr/include/c++/4.8.1/tuple:1050:7:   required from ‘void std::swap(std::tuple<_Elements ...>&, std::tuple<_Elements ...>&) [with _Elements = {addrinfo*, __lambda0}]’
/usr/include/c++/4.8.1/bits/unique_ptr.h:269:21:   required from ‘void std::unique_ptr<_Tp, _Dp>::swap(std::unique_ptr<_Tp, _Dp>&) [with _Tp = addrinfo; _Dp = __lambda0]’
/usr/include/c++/4.8.1/bits/unique_ptr.h:484:7:   required from ‘void std::swap(std::unique_ptr<_Tp, _Dp>&, std::unique_ptr<_Tp, _Dp>&) [with _Tp = addrinfo; _Dp = __lambda0]’
mini.cpp:21:29:   required from here
/usr/include/c++/4.8.1/bits/move.h:176:11: erreur: use of deleted function ‘__lambda0& __lambda0::operator=(const __lambda0&)’
       __a = _GLIBCXX_MOVE(__b);
           ^
mini.cpp:9:17: note: a lambda closure type has a deleted copy assignment operator
 auto deleter = [](struct addrinfo* ptr)
                 ^
In file included from /usr/include/c++/4.8.1/bits/stl_pair.h:59:0,
                 from /usr/include/c++/4.8.1/bits/stl_algobase.h:64,
                 from /usr/include/c++/4.8.1/memory:62,
                 from mini.cpp:1:
/usr/include/c++/4.8.1/bits/move.h:177:11: erreur: use of deleted function ‘__lambda0& __lambda0::operator=(const __lambda0&)’
       __b = _GLIBCXX_MOVE(__tmp);
           ^
4

4 回答 4

5

unique_ptr这与or无关tuple,您可以将错误减少为:

int main()
{
  auto deleter = []() { };
  auto del2 = deleter;
  deleter = static_cast<decltype(deleter)>(del2);
}

使用 Clang 编译但使用 G++ 失败,出现以下错误:

t.cc: In function ‘int main()’:
t.cc:5:11: error: use of deleted function ‘main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&)’
   deleter = static_cast<decltype(deleter)>(del2);
           ^
t.cc:3:19: note: a lambda closure type has a deleted copy assignment operator
   auto deleter = []() { };
                   ^

最后一个 C++11 标准在 [expr.prim.lambda]/19 中说:

与 lambda 表达式关联的闭包类型具有已删除的 (8.4.3) 默认构造函数和已删除的复制赋值运算符。它有一个隐式声明的复制构造函数(12.8),并且可能有一个隐式声明的移动构造函数(12.8)。

因此,类型是否可移动分配取决于编译器。

于 2013-07-05T23:14:51.080 回答
3

扩展Jonathan Wakely 的回答

当您交换到unique_ptrs 时,您还必须交换它们的删除器。您看到的问题归结为:clang 可以交换两个相同类型的 lambda,而 gcc 不能(并且标准允许两者,正如 Jonathan 引用的那样)。示范:

#include <utility>

int main() {
  auto f = [](){};
  auto g(f);
  std::swap(f, g);
}

此代码适用于 clang,但无法使用 gcc 编译。(没关系。)

这就是为什么它正在发生。


我建议如下:

#include <memory>
#include <utility>

struct addrinfo { };

void freeaddrinfo(addrinfo* ) { }

struct deleter {
  void operator()(struct addrinfo* ptr) {
    if (ptr != nullptr)
      freeaddrinfo(ptr);
  }
};

using resources_keeper = std::unique_ptr<struct addrinfo, deleter>;

int main() {

    resources_keeper plouf1(nullptr);
    resources_keeper plouf2(nullptr);

    std::swap(plouf1, plouf2);
    return 0;
}

请注意,代码也变得更清晰、更易读。


如果你绝对必须用 lambdas 来解决这个问题,那么也许你可以尝试一些类似这样的hackish:只交换指针而不交换删除器。

#include <iostream>
#include <memory>
#include <utility>

using namespace std;

template <class T, class D>
void swap_pointers_but_not_deleters(unique_ptr<T,D>& x, unique_ptr<T,D>& y) noexcept {

  T* x_ptr = x.release();

  x.reset(y.release());

  y.reset(x_ptr);
}

int main() {

  auto deleter = [](int* p){ delete p; };

  unique_ptr<int,decltype(deleter)> a(new int(1),deleter);

  unique_ptr<int,decltype(deleter)> b(new int(2),deleter);

  swap_pointers_but_not_deleters(a, b);

  cout << "a = " << *a << ", b = " << *b << endl;
}

虽然这段代码似乎可以工作,但我真的不喜欢它。我建议第一个不使用 lambdas 的解决方案。

于 2013-07-05T21:47:50.553 回答
2

我可以使用以下代码重现类似的错误:

struct A
{
    A() = default;
    A(A&&) = default;
    //A & operator=(A&&) = default;
    A(A const & ) = delete;
};

int main()
{
    A a, b;
    std::swap(a,b);
}

取消注释移动赋值运算符,错误就会消失。我猜gcc不允许lambas的移动分配(我使用的是4.7.2版)。将 lambda 更改为实际的函数或仿函数,你应该没问题。

于 2013-07-05T17:36:46.053 回答
1

事实证明,您可以使用 lambdas 解决它,只要它们可以转换为函数指针(lambdas 什么都不捕获)。

#include <memory>
#include <utility>

struct addrinfo { };

void freeaddrinfo(addrinfo* ) { }

auto deleter = [](struct addrinfo* ptr) {
  if (ptr != nullptr)
    freeaddrinfo(ptr);
};

using resources_keeper = std::unique_ptr<struct addrinfo, void(*)(struct addrinfo*)>;

int main() {

    resources_keeper plouf1(nullptr,deleter);
    resources_keeper plouf2(nullptr,deleter);

    std::swap(plouf1, plouf2);
    return 0;
}

但是,我仍然更喜欢结构的其他解决方案。它可能是最有效的一种(由于内联),其次是此处介绍的解决方案。如果删除器的实现真的很简单,那么传递一个重量级std::function对我来说似乎有点过头了。这些性能考虑是否重要,这是分析器的工作。

于 2013-07-06T13:27:09.130 回答