Compile errors using tr1::bind

This code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <algorithm>
#include <tr1/functional>
using std::ostream;

class Value
{
public:
    Value() : m_amount(0) {}
    
    void IncreaseBy(int amount) { m_amount += amount; }
    
    void WriteTo(ostream& stream) const { stream << m_amount; }
    
private:
    int m_amount;
};

ostream& operator <<(ostream& stream, const Value& value)
{
    value.WriteTo(stream);
    return stream;
}

int main()
{
    Value sum;
    
    int values[] = { 1, 2, 3, 4, 5 };
    
    std::for_each(values, values + 5, std::tr1::bind(&Value::IncreaseBy, sum));
    
    std::cout << "The value sum is " << sum << std::endl;
    
    return 0;
}

Produces compiler error output like the following on Fedora 10 (Intel):

/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional: In member function ‘typename std::tr1::result_of<_Functor ()(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 0)> ()(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor ()(_Bound_args ...)>::__call(const std::tr1::tuple<_UElements ...>&, std::tr1::_Index_tuple<_Indexes ...>) [with _Args = int&, int ..._Indexes = 0, _Functor = std::tr1::_Mem_fn<void (Value::*)(int)>, _Bound_args = Value]’:
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional:1192: instantiated from ‘typename std::tr1::result_of<_Functor ()(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 0)> ()(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor ()(_Bound_args ...)>::operator()(_Args& ...) [with _Args = int, _Functor = std::tr1::_Mem_fn<void (Value::*)(int)>, _Bound_args = Value]’
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h:3791: instantiated from ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = std::tr1::_Bind<std::tr1::_Mem_fn<void (Value::*)(int)> ()(Value)>]’
ValueSum.cpp:31: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional:1138: error: no match for call to ‘(std::tr1::_Mem_fn<void (Value::*)(int)>) (Value&)’
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional:551: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void, _Class = Value, _ArgTypes = int]
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional:556: note: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void, _Class = Value, _ArgTypes = int]
/usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/tr1_impl/functional:1138: error: return-statement with a value, in function returning 'void'

What is needed so this code will compile and run properly?
std::tr1::bind( &Value::IncreaseBy, sum, _1 );

if that doesn't work, use &sum instead.

i don't know if _1 is global or if you need to prefix it as well; i'm used
to using boost::bind.

also, why aren't you just using std::accumulate?

std::cout << "The value sum is " << std::accumulate( values, values + 5, 0 ) << std::endl;


(defined in <numeric>)
Topic archived. No new replies allowed.