Using std :: bind2nd with links

I have a simple class:

class A
{
public:
    void f(const int& n)
    {
        std::cout<<"A::f()" << n <<"\n";
    }
};

and I am trying to use it as follows:

std::vector<A> vec;
A a;
vec.push_back(a);
std::for_each(vec.begin(), vec.end(), std::bind2nd(std::mem_fun_ref(&A::f), 9));

But when I compile the code, I get the following error somewhere inside the function header file:

error C2529: '_Right': link to link is illegal

If I delete the link in the f () parameter, it compiles fine. How to resolve this? I do not want to delete the link, as in my real code, copying an object is quite expensive. In addition, I do not use boost.

+3
source share
4 answers

, . , std::bind1st std::bind2nd ( ..). Boost - boost::bind , boost::ref.

TR1 - g++ V++ 2008 SP1 - std::tr1::bind, , boost::bind, .

+5

, , . ( STL, , , )

.

struct CallF
{
    CallF(int const& data): m_data(data)    {}
    void operator()(A& val) const
    {
        val.f(m_data);
    }
    int const& m_data;
};

:

    std::for_each(vec.begin(), vec.end(), CallF(9));
+2

. ++, , " ". ++ . mem_fun_ref typedefs (

argument_type, first_argument_type, second_argument_type

), . bind1st bind2nd (), . argument_type , .

memfunref type_type.

+1

, :

C2529: '_Right':

std :: binders accept their arguments as links - you cannot pass a link to a link.

In no case.

0
source

Source: https://habr.com/ru/post/1718358/


All Articles