#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void push_back(int v, vector<int>& coll)
{
coll.push_back(v);
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
std::vector<int> b;
for_each(a, a + 5, bind2nd(ptr_fun(push_back), b));
}
编译器说:
/usr/include/c++/4.1.2/bits/stl_function.h: In instantiation of ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >’:
tt5.cpp:15: instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:435: error: forming reference to reference type ‘std::vector<int, std::allocator<int> >&’
/usr/include/c++/4.1.2/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>, _Tp = std::vector<int, std::allocator<int> >]’:
tt5.cpp:15: instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:455: error: no matching function for call to ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>&, std::vector<int, std::allocator<int> >&)’
/usr/include/c++/4.1.2/bits/stl_function.h:429: note: candidates are: std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >&)