G
Giovanni Gherdovich
Hello,
in the following code I have a pointer (to function), say p,
of type
double (*)(double, double, void*)
and I try to fix the second argument of the function *p
to a given value (using boost::bind), but the compiler
complains, because of a type mismatch in an assignment
which I think should be legal:
/*
* bash-3.00$ g++ function.cpp
* function.cpp: In function `int main()':
* function.cpp:36: warning: taking address of temporary
* function.cpp:36: error: cannot convert
* `boost::_bi::bind_t<double,
* double (*)(double, double, void*),
* boost::_bi::list3<boost::arg<1>,
*
boost::_bi::value<double>,
* boost::arg<2> > >*'
* to `double (*)(double, void*)' in assignment
*/
Can you see why? The code is below.
There is also a warning which I don't like at all,
and remebers me that boost::bind( ... ) is a temporary
object and its address makes sense only in the current
scope (if I'm not wrong).
Since I have to pass this address as argument to another
function (later, in my real code), this can couse problems.
How can I build a "real" (I mean non-temporary) object
out of boost::bind( ... ), so that its address can
be passed forth and back across my code?
Regards,
Giovanni
// ---------------------------------------------- the file
function.cpp
#include <boost/bind.hpp>
struct oneVar_function // one variable function
{
double (* function) (double x, void* params);
void* params;
};
struct twoVar_function // two variables function
{
double (* function) (double x, double y, void* params);
void* params;
};
double
my_function (double x, double y, void* params)
{
// I cast `params` to double*.
// The user will be kind enough to always
// give a double* as 3rd argument when
// calling my_function.
double* alpha = static_cast<double*>(params);
return x + y + *alpha; // or whatever
}
int main()
{
twoVar_function f;
oneVar_function g;
f.function = &my_function;
double alpha = 3;
f.params = α
double a_given_number = 2;
g.function = &(boost::bind(*(f.function),
_1,
a_given_number,
_2));
g.params = α
}
// -------------------------------------- end of the file function.cpp
in the following code I have a pointer (to function), say p,
of type
double (*)(double, double, void*)
and I try to fix the second argument of the function *p
to a given value (using boost::bind), but the compiler
complains, because of a type mismatch in an assignment
which I think should be legal:
/*
* bash-3.00$ g++ function.cpp
* function.cpp: In function `int main()':
* function.cpp:36: warning: taking address of temporary
* function.cpp:36: error: cannot convert
* `boost::_bi::bind_t<double,
* double (*)(double, double, void*),
* boost::_bi::list3<boost::arg<1>,
*
boost::_bi::value<double>,
* boost::arg<2> > >*'
* to `double (*)(double, void*)' in assignment
*/
Can you see why? The code is below.
There is also a warning which I don't like at all,
and remebers me that boost::bind( ... ) is a temporary
object and its address makes sense only in the current
scope (if I'm not wrong).
Since I have to pass this address as argument to another
function (later, in my real code), this can couse problems.
How can I build a "real" (I mean non-temporary) object
out of boost::bind( ... ), so that its address can
be passed forth and back across my code?
Regards,
Giovanni
// ---------------------------------------------- the file
function.cpp
#include <boost/bind.hpp>
struct oneVar_function // one variable function
{
double (* function) (double x, void* params);
void* params;
};
struct twoVar_function // two variables function
{
double (* function) (double x, double y, void* params);
void* params;
};
double
my_function (double x, double y, void* params)
{
// I cast `params` to double*.
// The user will be kind enough to always
// give a double* as 3rd argument when
// calling my_function.
double* alpha = static_cast<double*>(params);
return x + y + *alpha; // or whatever
}
int main()
{
twoVar_function f;
oneVar_function g;
f.function = &my_function;
double alpha = 3;
f.params = α
double a_given_number = 2;
g.function = &(boost::bind(*(f.function),
_1,
a_given_number,
_2));
g.params = α
}
// -------------------------------------- end of the file function.cpp