我有以下代码:
#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
似乎虽然绑定对象有一个复制构造函数,但它的复制赋值运算符被删除了。如果我尝试使用 lambda 而不是bind
.
这是 C++11 标准、libstdc++ 实现还是 Boost 适配器实现中的错误?
最好的解决方法是什么?我可以把它包装成一个
std::function
. 似乎boost::bind
也有效。哪个更有效,还是真的很重要?