Answer the question
In order to leave comments, you need to log in
std::bind. Bind to member data?
I wanted to try a beautiful and powerful std::bind
(more precisely std::tr1::bind
, you still need to work with the old standard, the Windows platform, msvc10), which, in comparison std::bind1st
and std::bind2nd
, among other things, solves the problem of linking to a link ... and you need to wean yourself from old binders.
I liked the fact that with the help std::bind
you can reach some data member, i.e., for example:
struct Data
{
int d1;
double d2;
// ...
};
using namespace std::placeholders;
// по сути, мы создали функцию, которая принимает экземпляр Data и возвращает значение Data::d2
std::bind(&Data::d2, _1);
// т.е.
Data myData = { 1, 10.0 };
// value == 10.0
double value = std::bind(&Data::d2, _1)(myData);
Data
) whose d2 value is equal to some value. So, with the help of std::bind2nd
:struct MyPredicate
: std::binary_function<Data, double, bool>
{
bool operator()(Data d, double value) const
{
return (d.d2 == value);
}
};
// ...
enum { SIZE = 10 };
Data arr[SIZE] = {};
const Data* const begin = arr;
const Data* const end = arr + SIZE;
double value = 0.0;
const Data* const pos = std::find_if(begin, end, std::bind2nd(MyPredicate(), value));
// ...
std::bind
-a, all this can be made easier, it would seem:const Data* const pos = std::find_if(begin, end,
std::bind(std::equal_to<double>(), std::bind(&Data::d2, _1), value));
std::bind
returns the address of a local variable somewhere and &Data::d2
an invalid pointer is passed instead. Answer the question
In order to leave comments, you need to log in
About bind and msvc: it's very badly written and contains a lot of bugs, so you may not be surprised by its strange behavior in the studios (including 2012)
I am a complete stranger and no one needs it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question