c++ - 这是什么构造:模板无效函数名称(int i)?
问题描述
我在编写模板函数特化时不小心犯了一个错误,结果构造通过了 VS17 的编译。(下面包含的代码中的第三个构造)
这是一个有效的构造吗?我该如何调用这个函数?
template <class T> void tempfunc(T t)
{
cout << "Generic Template Version\n";
}
template <>
void tempfunc<int>(int i) {
cout << "Template Specialization Version\n";
}
template <int> void tempfunc(int i)
{
cout << "Coding Mistake Version\n";
}
我无法调用第三个构造。
解决方案
是的,这是一个有效的构造。这是一个模板重载,它是在 type 的非类型模板参数上模板化的int
。
你可以这样称呼它:
tempfunc<42>(42);
请注意,没有模板语法的调用仍将调用在类型参数上模板化的版本:
tempfunc(42); // calls specialization
tempfunc(true); // calls primary
这是一个演示
推荐阅读
- php - 对 Laravel 查询构建器的 SQL 查询
- r - 将数据帧的每一列除以数据帧的一行
- vba - VBA 代码的结果在 Access 的文本框中不可见
- performance - Flutter:大画布的缩放会降低 FPS
- spring - Spring Data JPA:参数化@Query注解字符串,重构两个相似的@Query方法
- azure-cognitive-services - 如何以编程方式关闭沉浸式阅读器?
- .net - 正确处理 dbnull 值
- r - 如何在 `mutate_at` 中应用一个函数,以按行对其他列中的值进行条件?
- javascript - jQuery Autocomplete,不同的来源取决于输入的字符
- python - 尝试在 Databricks 环境中合并或合并两个 pyspark.sql.dataframe.DataFrame