skyeagle
skyeagle

Reputation: 3271

C++ function template question

I was searching on the best way to lcase/ucase a C++ STL class and I came across this post:

STL string to lower case

One of the solutions given was:

#include <algorithm>
#include <string> 

std::string data = “Abc”; 
std::transform(data.begin(), data.end(), data.begin(), ::tolower);

However, transform is defined in stl_algo.h as:

  template<typename _InputIterator, typename _OutputIterator,
       typename _UnaryOperation>
    _OutputIterator
    transform(_InputIterator __first, _InputIterator __last,
          _OutputIterator __result, _UnaryOperation __unary_op)
    {
...

So how come it is being called without providing the template instantiation parameters?

To clarify my question, I was expecting the transform function to be called like:

transform(std::string::iterator, std::string::iterator, 
          /* not sure what to put here for the predicate */);

Is this a one off (a special case), or am I missing something fundamental?

Upvotes: 0

Views: 150

Answers (2)

wich
wich

Reputation: 17117

Template parameters are implicitly derived from the function arguments.

Upvotes: 1

Prasoon Saurav
Prasoon Saurav

Reputation: 92854

This is called Template Argument Deduction.

Here is another nice article explaining Template Argument Deduction.

Upvotes: 5

Related Questions