大量C++书籍下载: c++ 20: free download. Ebooks library. On-line books store on Bookzz
大量机器学习书籍下载: machine learning: free download. Ebooks library. On-line books store on Bookzz
首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1。这个例子本身比较傻,但有不傻的应用。
template<typename T> T add(T a, T b) { return a + b; }
template<typename T> auto get_inc() {
T b = 1;
return bind(add<T>, std::placeholders::_1, b);
}
std::function<int(int)> inc = get_inc<int>();
printf("%d\n", inc(2));
其次为啥bind默认传copy而不是reference? get_inc()返回后, 它里面的b就没了,stack frame供别的函数用去了。传引用的话就引错地方了。
可有时候很需要传应用,如两个vector相加,而且我们知道引用是有效的:
#include <stdio.h>
#include <vector>
#include <functional>
using namespace std;
struct vec : public vector<int> {
vec() {}
vec(const vec&) { puts("vec(const vec&)"); }
vec& operator=(const vec&) { puts("operator=(const vec&)"); return *this; }
};
void addv(const vec& a, const vec& b) {} // 没有返回值不重要
int main() {
std::function<int(int)> inc = get_inc<int>();
printf("%d\n", inc(2));
vec a, one;
auto incv = bind(addv, std::placeholders::_1, ref(one));
incv(a);
}
如果把ref(one)换成one,可看到copy constructor被调用。可以在某个函数里new个vec* v, 再ref(*v),最后某处delete v.
std::ref是如何实现的?Function templates ref and cref are helper functions that generate an object of type std::reference_wrapper, using template argument deduction to determine the template argument of the result. [cppreference]
How does c++11 std::ref work? - Stack Overflow 我搞了个指针版:
template<class T> struct myref_wrapper { T* p; myref_wrapper(T& t) : p(&t) {} T& operator()() { return *p; } }; template<class T> myref_wrapper<T> myref(T& t) { return myref_wrapper<T>(t); } int i = 0; myref(i)() = 1; printf("%d\n", i);
真是人生苦短,我学python啊。c++ - Implementation of std::reference_wrapper std::bind简单使用 - 博客园 Passing reference with std::ref in C++
浙公网安备 33010602011771号