
Hello there! I'm about to explore the power of boost::bind, but I am still a beginner. Maybe the question is rather stupid, but the following code does not compile and I do not understand why: typedef std::vector<std::size_t> data_t; data_t dataVec; ... populate dataVec ... std::size_t shiftIndex = 3; boost::function<std::size_t& (std::size_t)> shiftedData = boost::bind(&data_t::at, &dataVec, _1 - shiftIndex); The error message is something like /home/sweber/graphpkg/libs/boost/boost/function/function_template.hpp:119: error: invalid initialization of reference of type 'size_t&' from expression of type 'const unsigned int' My system is a debian sarge with a g++-3.3 and boost 1.33.1. What I'm trying to do is to shift the index of the data-vector such that index 3 would address the first element. Thanks for any help. Greetings, Sebastian Weber

On 10/23/06, Sebastian Weber <sebastian.weber@physik.tu-darmstadt.de> wrote:
Hello there!
I'm about to explore the power of boost::bind, but I am still a beginner. Maybe the question is rather stupid, but the following code does not compile and I do not understand why:
typedef std::vector<std::size_t> data_t;
data_t dataVec;
... populate dataVec ...
std::size_t shiftIndex = 3;
boost::function<std::size_t& (std::size_t)> shiftedData = boost::bind(&data_t::at, &dataVec, _1 - shiftIndex);
The error message is something like
/home/sweber/graphpkg/libs/boost/boost/function/function_template.hpp:119: error: invalid initialization of reference of type 'size_t&' from expression of type 'const unsigned int'
My system is a debian sarge with a g++-3.3 and boost 1.33.1.
What I'm trying to do is to shift the index of the data-vector such that index 3 would address the first element.
Thanks for any help.
Greetings,
Sebastian Weber
Boos.Bind will only do the binding bit - what you're wanting to do (apply an expression to the parameter at call-time) can be accomplished with Boost.Lambda - which has its own bind facility (with pretty much exactly the same syntax as Boost.Bind) that you'll need to use instead of Boost.Bind. HTH Stuart Dootson

Hello!
Boos.Bind will only do the binding bit - what you're wanting to do (apply an expression to the parameter at call-time) can be accomplished with Boost.Lambda - which has its own bind facility (with pretty much exactly the same syntax as Boost.Bind) that you'll need to use instead of Boost.Bind.
Thanks for the hint, you are absolutely right. Nevertheless, it is a mess to use the lambda library with overloaded functions, as the correct call (which took me now about an hour to find out seems to be): boost::lambda::bind(static_cast<data_t::reference (data_t::*)(std::size_t)>(&data_t::at), &dataVec, boost::lambda::_1 - shiftedIndex) This really messy, I think. Greetings, Sebastian Weber
HTH Stuart Dootson _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

"Sebastian Weber" <sebastian.weber@physik.tu-darmstadt.de> wrote
I'm about to explore the power of boost::bind, but I am still a beginner. Maybe the question is rather stupid, but the following code does not compile and I do not understand why:
typedef std::vector<std::size_t> data_t;
data_t dataVec;
... populate dataVec ...
std::size_t shiftIndex = 3;
boost::function<std::size_t& (std::size_t)> shiftedData = boost::bind(&data_t::at, &dataVec, _1 - shiftIndex);
The following should work: boost::function<std::size_t& (std::size_t)> shiftedData = boost::bind(&data_t::at, &dataVec, boost::bind(std::minus<size_t>(), _1, shiftIndex)); HTH, Arkadiy
participants (3)
-
Arkadiy Vertleyb
-
Sebastian Weber
-
Stuart Dootson