未指定模板参数,但仍有效
no template argument specified but still works
这是STL make_heap
的函数声明,它期望随机访问迭代器作为参数
template <class RandomAccessIterator>
void make_heap (RandomAccessIterator first, RandomAccessIterator last);
但是当我们称之为:
std::make_heap (v.begin(),v.end());
我们没有指定template argument
,为什么会这样...?
C++是否自动检测我们在功能调用中传递的数据类型...?
这是implicit type-casting
吗?
基于函数参数确定模板参数的过程称为模板参数推导。
从C++标准
14.8.2 模板参数推导
1 当引用函数模板专用化时,所有 模板参数应具有值。这些值可以显式 指定,或者在某些情况下,从使用中推断或从 默认模板参数。
[ 示例:
void f(Array<dcomplex>& cv, Array<int>& ci) {
sort(cv); // calls sort(Array<dcomplex>&)
sort(ci); // calls sort(Array<int>&)
}
相关文章:
- 我如何知道作为参数的size_t在函数中是否有效?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 为什么模板参数中的双冒号有效?
- 函数参数的名称与调用函数时使用的变量相同是否有效?
- 为什么使用不匹配的参数调用重载函数仍然有效
- C++/11 auto 关键字是在更有效时推导参数进行按引用传递,还是始终按值传递?
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 自动模板类enable_if有效参数
- 带有"up-"和"center-vec3"参数的glm::rotate()的有效替代方案?
- 为什么重写方法并将 const 添加到参数类型有效
- 字符串或const char*,更有效地用作构造函数参数
- 在 pthread_create() 中使用局部变量作为参数有效吗?
- 如何有效地传递 std::vector<cv::P oint3f> 的子向量作为参数(不拥有函数)
- 作为模板非类型参数的 C 字符串在 gcc 6.3 中有效,但在 Visual Studio 2017(x64 为 19
- 如果 null 不是有效值,则使参数成为引用而不是指针
- Qt C++ - 创建 QList<Myclass> - 不是参数 'T 的有效模板类型参数
- 如何以有效的方式将输入参数传递给函数
- C++17 有效地将参数包参数与 std::array 元素相乘
- GLFW 错误 - GLFWwindow* 未被识别为有效参数
- 配置文件上——host参数的有效参数列表