Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 混合类和int的函数模板专门化_C++_Templates_Template Specialization - Fatal编程技术网

C++ 混合类和int的函数模板专门化

C++ 混合类和int的函数模板专门化,c++,templates,template-specialization,C++,Templates,Template Specialization,我正在学习模板专门化,但无法理解混合类和int 以下代码无法编译。有人能给我推荐一条正确的路吗。我想专攻int类。第二个模板m应定义为0,但如何指定它 #include <iostream> using namespace std; template <class T,int m> void fun(T a ) { cout << "The main template fun(): " << a << " " << m

我正在学习模板专门化,但无法理解混合类和int

以下代码无法编译。有人能给我推荐一条正确的路吗。我想专攻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);
}
#包括
使用名称空间std;
模板
虚无乐趣(T a)
{

cout我建议改变参数的顺序,让T被推导,然后简单地使用重载:

template <int m, class T>
void fun(T a )
{
    cout << "The main template fun(): " << a  << "  " << m << endl;
}

template <int m>
void fun(int a)
{
    cout << "Template for int type: " << a << endl;
}
模板
虚无乐趣(T a)
{

cout我建议改变参数的顺序,让T被推导,然后简单地使用重载:

template <int m, class T>
void fun(T a )
{
    cout << "The main template fun(): " << a  << "  " << m << endl;
}

template <int m>
void fun(int a)
{
    cout << "Template for int type: " << a << endl;
}
模板
虚无乐趣(T a)
{

cout我假设您试图做的是专门化模板,以便任何形式的调用

fun<int, N>(...);
然后,您可以提供使用函数对象的包装器函数模板:

template <typename T, int N>
void fun(T a) {
    do_fun<T, N>{}(a);
}
模板
虚无乐趣(T a){
多芬(a);
}

我假设您要做的是专门化模板,以便表单的任何调用

fun<int, N>(...);
然后,您可以提供使用函数对象的包装器函数模板:

template <typename T, int N>
void fun(T a) {
    do_fun<T, N>{}(a);
}
模板
虚无乐趣(T a){
多芬(a);
}

您不能部分地专门化函数。您不能部分地专门化函数。