boost::bind with const parameter

Hi group! I try to call in a for_each a member function that take a parameter by value AND const. I know we do not need to do things like that, but I am using code from an other team and I don't want to change it if I don't need. It results with this error : D:\ENG-AC\working\tests\test_ParamConst_vs_boostBind\test\test.cpp(35) : error C2665: 'bind' : none of the 2 overloads can convert parameter 1 from type 'int (__thiscall test_class::*)(const int) const' Here a little program that reproduce de behavior : #include <algorithm> #include <vector> #include "boost/bind.hpp" class test_class { public: test_class(int value) : m_value(value){} ~test_class(){} int fct_param_const(const int value) const {return m_value + value;} int fct_param_ref_const(const int& value) const {return m_value + value;} int fct_param(int value) const {return m_value + value;} private: int m_value; }; int main(int argc, char* argv[]) { std::vector<int> all_values(5); std::generate(all_values.begin(), all_values.end(), &rand); test_class doTest(28); std::for_each(all_values.begin(), all_values.end(), boost::bind(&test_class::fct_param, doTest, _1)); std::for_each(all_values.begin(), all_values.end(), boost::bind(&test_class::fct_param_ref_const, doTest, _1)); // DOESN'T WORK : because of the parameter by value and const??? //std::for_each(all_values.begin(), all_values.end(), boost::bind(&test_class::fct_param_const, doTest, _1)); return 0; } Is this a correct behavior from boost::bind? Does the only solution is to remove the const from our function? We have so much difficulty to stay const-correct, I do not want to send a signal to our team that const isn't good (but maybe too much const isn't good :D). Anyway, what do you suggest? By the way. I am using VC6 (SP5) under Win2000 and boost 1.33.1. Regards, -- Alain Cormier alain.work@gmail.com

Alain Cormier wrote: [...]
int fct_param_const(const int value) const { return m_value + value; }
[...]
Is this a correct behavior from boost::bind? Does the only solution is to remove the const from our function? We have so much difficulty to stay const-correct, I do not want to send a signal to our team that const isn't good (but maybe too much const isn't good :D). Anyway, what do you suggest?
By the way. I am using VC6 (SP5) under Win2000 and boost 1.33.1.
This is a bug in VC6, see http://boost.org/libs/bind/bind.html#err_const_arg Top-level const in a function argument list is a peculiar thing. It is ignored for the purposes of the function signature, so void f( int const ); and void f( int ); actually declare the same function, and there is no such thing as void (*)( int const ), the correct type is void (*) ( int ). Such a const qualifier is only relevant on a function definition and makes the actual argument const. It has no impact on the const correctness of the caller side since a by-value parameter cannot affect the argument. boost::bind can do nothing about the fact that VC6 is confused by such functions, except maybe to encourage you to upgrade. :-)
participants (2)
-
Alain Cormier
-
Peter Dimov