Question: I have a class which basically just manages a vector of custom types. To relieve me from writing the same iteration loop over and over again I wrote the following construct:

```
template<typename T>
uint64_t ACCUMULATE_ON_VECTOR(const std::vector<MyClass*> vec, T method)
{
  return std::accumulate(vec.begin(), vec.end(), 0, [&](uint64_t acc, const MyClass* c)
  {
    return acc + (c ? method(c) : 0);
  });
}

```

which is then called like this:

```
ACCUMULATE_ON_VECTOR(_myVec, std::bind(&MyClass::someMethod, std::placeholders::_1));

```

This works very well in concept but since I have a lot of methods returning different integer types (signed/unsigned, signed/unsigned long), I'd like to abstract away the for now hardcoded uint64\_t cause I get compiler warnings all over the place. For this I somehow need to get the return type of the bind object. Can I somehow do this with decltype? What I'm looking for is this:

```
template<typename T>
<new deduced type> ACCUMULATE_ON_VECTOR(const std::vector<MyClass*> vec, T method)
{
  return std::accumulate(vec.begin(), vec.end(), 0, [&](<new deduced type> acc, const MyClass* c)
  {
    return acc + (c ? method(c) : 0);
  });
}

```

Answer: 
[`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) has a member type called `result_type` which is

> 
> 1) If F is a pointer to function or a pointer to member function, result\_type is the return type of F. If F is a class type with nested typedef result\_type, then result\_type is F::result\_type. Otherwise no result\_type is defined.
> 
> 
> 2) result\_type is exactly R.
> 
> 
> 

We can use a trailing return type like

```
template<typename T>
auto ACCUMULATE_ON_VECTOR(const std::vector<MyClass*> vec, T method) -> typename T::result_type
{
  typename T::result_type begin = 0;
  return std::accumulate(vec.begin(), vec.end(), begin, [&](typename T::result_type acc, const MyClass* c)
  {
    return acc + (c ? method(c) : 0);
  });
}

```

This will work with C++11 and above.

I also added `typename T::result_type begin = 0;` to the function so the type passed to accumulate is the same type as the return of the function. As you just had `0` that gets deduced to an `int` and `accumulate` will always return an `int`.
The result type of accumulate has nothing to do with the passed accumulating function. **The result type of accumulate is the type of the third parameter.** You're getting warnings because your third parameter is `0` (which is int) so the `int64` will get truncated to `int` anyway! You should pass `int46(0)` as your third parameter for a warning-free code.