Take the 2-minute tour ×

I was trying to write an STL method to take the log of a vector:


But I get

no matching function for call to ‘for_each(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, <unresolved overloaded function type>)’

Which I gather is due to log function's multiple versions. Obviously I can write a simple wrapper around the log function and call it with that. Is there a simpler way to specify which log function I want inline?

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

Yes. You can cast the function to the appropriate type:


Another possibility would be creating your functor that would accept any type:

struct log_f
  template <class T> T operator()(const T& t) const { return log(t); }

for_each(vec.begin(),vec.end(), log_f());

And, as Billy O'Neal pointed out, you want rather transform than for_each.

share|improve this answer
+1 and scratched out that part of my answer -- this is the correct answer here. –  Billy ONeal Aug 4 '10 at 0:17
Beautiful, I was originally using transform, but I switched to for_each forgetting that it had different functionality. Thanks, I figured it was some sort of dynamic cast, I just didn't know what the syntax would be. Just so I'm clear that's saying cast log to a function that returns a double and takes a double. Also what does the (*) specify to the compiler in the middle? –  JSchlather Aug 4 '10 at 0:33
This is static_cast. the (*) in the middle means pointer. ie. void(int) is a type of a function that takes an int and returns nothing, void(*)(int) is a pointer to that function. void*(int) would be a function (not a pointer) that takes an int and returns a void-pointer. –  jpalecek Aug 4 '10 at 0:41
Ah, makes sense. Thanks again. –  JSchlather Aug 4 '10 at 0:46
It does help to write the static_cast explicitly: for_each(vec.begin(),vec.end(), static_cast< double(*)(double) >(log)); –  MSalters Aug 4 '10 at 7:25
add comment

I believe std::for_each is looking for a function with a void return type. You're passing a function with a double return type. jpalecek's answer is the correct one, and +1 to him. However, you still have the semantic issue that doing for_each with log doesn't make any sense:

If you want all the members of the vector to be the log of the previous members, i.e.:

foreach( var x in myvector )
   x = log(x);

Then you don't want for_each, you want transform.

std::transform(vec.begin(), vec.end(), vec.begin(), log);
share|improve this answer
add comment

Your Answer


