C++函数模板问题
我正在寻找 lcase/ucase C++ STL 类的最佳方法,我发现了这篇文章:
给出的解决方案之一是:
#include <algorithm>
#include <string>
std::string data = “Abc”;
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
但是,transform 在 stl_algo.h 中定义为:
template<typename _InputIterator, typename _OutputIterator,
typename _UnaryOperation>
_OutputIterator
transform(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _UnaryOperation __unary_op)
{
...
那么为什么在不提供模板实例化参数的情况下调用它呢?
为了澄清我的问题,我期望转换函数的调用方式如下:
transform(std::string::iterator, std::string::iterator,
/* not sure what to put here for the predicate */);
这是一次性的(一种特殊情况),还是我错过了一些基本的东西?
I was searching on the best way to lcase/ucase a C++ STL class and I came across this post:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这称为 模板参数推导。
这里是另一篇解释模板参数推导的好文章。
This is called Template Argument Deduction.
Here is another nice article explaining Template Argument Deduction.
模板参数是从函数参数隐式派生的。
Template parameters are implicitly derived from the function arguments.