开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜