0

次のコードがあります。

#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm.hpp>

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

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

struct B {
    B() = default;
    B(const B&) = delete;
    B& operator=(const B&) = delete;

    int foo(const A&, int b)  {
        return -b;
    }
};

int main() {
    A a;
    auto b = std::make_shared<B>();
    std::vector<int> values{1, 2, 3, 2};

    using std::placeholders::_1;
    auto fun = std::bind(&B::foo, b.get(), std::ref(a), _1);
    int min = *boost::min_element(values | boost::adaptors::transformed(fun));
    std::cout << min << std::endl;
}

コンパイルしようとすると、clang から次のエラー メッセージが表示されます (完全な出力はこちら)。

/usr/local/include/boost/optional/optional.hpp:674:80: error: object of type 'std::_Bind<std::_Mem_fn<int (Base::*)(const A &, int)> (Base *, std::reference_wrapper<A>, std::_Placeholder<1>)>' cannot be assigned because its copy assignment operator is implicitly deleted

バインド オブジェクトにはコピー コンストラクターがありますが、そのコピー代入演算子は削除されているようです。の代わりにラムダを使用しようとすると、同じエラーが発生しますbind

  1. これは、C++11 標準、libstdc++ 実装、または Boost アダプター実装のバグですか?

  2. これに対する最善の回避策は何ですか? にラップできstd::functionます。それboost::bindも効いているようです。どちらがより効率的ですか、それとも本当に重要ですか?

4

1 に答える 1