【发布时间】:2017-08-05 10:48:16
【问题描述】:
我正在学习模板专业化但无法理解混合类和int。
以下代码无法编译click to compile。有人可以在这里提出正确的方法。我希望专攻int类。第二个模板 m 应该定义为 0 但如何指定。
#include <iostream>
using namespace std;
template <class T,int m>
void fun(T a )
{
cout << "The main template fun(): " << a << " " << m << endl;
}
template<>
void fun(int a)
{
cout << "Specialized Template for int type: " << a << endl;
}
int main()
{
fun<char,10>('a');
fun<int,20>(10);
fun<float,12>(10.14);
}
错误是:
prog.cpp:11:6: error: template-id 'fun<>' for 'void fun(int)' does not match any template declaration
void fun(int a)
^
【问题讨论】:
-
您不能随意对函数进行部分特化。
标签: c++ templates template-specialization