我正在研究模板专业化,但无法理解混合类和int。混合类和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)
^
你不能部分专门化功能,只要你想。 – Jarod42