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?
This is called Template Argument Deduction.
Here is another nice article explaining Template Argument Deduction.
Template parameters are implicitly derived from the function arguments.
精彩评论