注册 登录
编程论坛 VC++/MFC

模板函数显式具体化的问题

LOV迪迪 发布于 2012-07-16 09:53, 946 次点击
#include<iostream>
#include<cstring>
template<class T>
const T Maxn(const T arr[],int n);                        //模板函数
//
template<>const char* Maxn(const char* arr[],int n);       //模板函数显示具体化,此行报错
using namespace std;
int main()
{
int num[4]={1,2,3,4};
double dig[6]={1.1,2.2,3.3,4.4,5.5,6.6};
cout<<"The max of 4 int:"<<Maxn(num,4)<<endl<<endl;
cout<<"The max of 6 double:"<<Maxn(dig,6)<<endl<<endl;      //int,double数组来测试模板函数运行
char* pc[5]={
"nnnnnnnnnnnnn",
"nnn is a handsome and excellent boy!",
"wwwwwwwwwwwwwwww",
"sssssssssssss",
"swdefsfsdf"
};                                                          //创建指针数组。每个数组元素是指向一个字符串的指针
cout<<pc[1]<<endl<<endl<<endl;
cout<<"Your longest string is:\n"
<<Maxn(pc,5)<<endl;                                         //此处以指针数组名为参数,调用的应该是显示具体化的函数
return 0;
}
template<class T>
const T Maxn(const T arr[],int n)                          //模板函数体
{
T max=arr[0];
for (int i=1;i<n;i++)
if (max<arr[i])
max=arr[i];
return max;
}
template<>const char* Maxn(const char* pc[],int n)          //模板函数显示具体化,此行报错
{                                             
const char* pm=pc[0];
for (int i=1;i<n;i++)
if (strlen(pm)<strlen(pc[i]))
pm=pc[i];
return pm;
}
我是实在不知道自己错在哪里了,模板函数显示具体化,之前用着一直没事的啊,怎么这次我怎么看都不知道错在哪,程序编译无法通过,错误如下:
==============================================================
C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(6) : error C2912: explicit specialization; 'const char *__cdecl Maxn(const char *[],int)' is not a function template
        C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(6) : see declaration of 'Maxn'
C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(39) : error C2912: explicit specialization; 'const char *__cdecl Maxn(const char *[],int)' is not a function template
        C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(6) : see declaration of 'Maxn'
C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(39) : error C2912: explicit specialization; 'const char *__cdecl Maxn(const char *[],int)' is not a function template
        C:\Program Files\Microsoft Visual Studio\MyProjects\程序文本\8.8.6.cpp(6) : see declaration of 'Maxn'
=========================================================
总是在我的显示具体化函数那里报错,说他不是一个函数模板,什么意思啊
求高手赐教

[ 本帖最后由 LOV迪迪 于 2012-7-16 10:01 编辑 ]
4 回复
#2
peach54602012-07-16 13:15
程序代码:

#include<iostream>
#include<cstring>
template<class T>
const T Maxn(const T arr[],int n);                        //模板函数
//
template<class T>
const char* Maxn(const char* arr[],int n);       //模板函数显示具体化,此行报错
using namespace std;
int main()
{
    int num[4]={1,2,3,4};
    double dig[6]={1.1,2.2,3.3,4.4,5.5,6.6};
    cout<<"The max of 4 int:"<<Maxn(num,4)<<endl<<endl;
    cout<<"The max of 6 double:"<<Maxn(dig,6)<<endl<<endl;      //int,double数组来测试模板函数运行
    char* pc[5]={
        "nnnnnnnnnnnnn",
        "nnn is a handsome and excellent boy!",
        "wwwwwwwwwwwwwwww",
        "sssssssssssss",
        "swdefsfsdf"
    };                                                          //创建指针数组。每个数组元素是指向一个字符串的指针
    cout<<pc[1]<<endl<<endl<<endl;
    cout<<"Your longest string is:\n"
        <<Maxn(pc,5)<<endl;                                         //此处以指针数组名为参数,调用的应该是显示具体化的函数
    return 0;
}
template<class T>
const T Maxn(const T arr[],int n)                          //模板函数体
{
    T max=arr[0];
    for (int i=1;i<n;i++)
        if (max<arr[i])
            max=arr[i];
    return max;
}
template<class T>
const char* Maxn(const char* pc[],int n)          //模板函数显示具体化,此行报错
{                                             
    const char* pm=pc[0];
    for (int i=1;i<n;i++)
        if (strlen(pm)<strlen(pc[i]))
            pm=pc[i];
    return pm;
}


VS2008SP1编译通过,你多看看模版的用法...
#3
LOV迪迪2012-07-20 11:28
回复 2楼 peach5460
你把我所有的函数显式具体化前缀template<>都改成了template<class T>,的确能编译通过了·····可是就失去了具体化的意义了,完全就是按照原模板函数在运行啊,运行结果输出最长字符串的那一项目输出的只是第一个字符串(这刚好是模板函数算法下的结果),但是用具体化的算法,返回的应该是最长的字符串
#4
peach54602012-07-20 12:39
我不知道是不是我模版没学到位
但是,我觉得
template<class T>
const char* Maxn(const char* pc[],int n) {}

或者
const char* Maxn(const char* pc[],int n) {}

都可以编译通过...
但是
template<>
const char* Maxn(const char* pc[],int n) {}
你又不用模版,你又指定模版函数...我没见过这样的用法

至于说具现化...
模版是在变异期间进行类型推断的...如果需要调用模版函数而非类型推断后的函数倒是可以template<> const char* Maxn(const char* pc[],int n) 这么调用
但是你不可能这么去定义函数...

[ 本帖最后由 peach5460 于 2012-7-20 12:47 编辑 ]
#5
皮小鹏2017-10-07 22:41
显式具体化格式不对
古老的帖子,今天遇到同样的问题,不晓得题主问题搞定没。以后或许有人遇到同样的问题看到这个帖子,我来回复一下啦!
#include "stdafx.h"


template<typename T>const T maxn(const T arr[],int n);
template<> char* const maxn(char* const arr[], int n);

int main()
{
    using namespace std;
    int int_arr[5]{ 2,3,2,1,5 };
    char* str_arr[3]{"i am pixiaopeng","my name is pixiaopeng","i love you"};
    double double_arr[5]{ 2.3,5.7,1.2,3.6,8.3 };
    cout << maxn(int_arr, 5) << endl
        << maxn(double_arr, 5) << endl
        << maxn(str_arr, 3) << endl;
    return 0;
}

template<typename T>const T maxn(const T arr[],int n)
{
    T temp = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (arr[i]>temp)
        {
            temp = arr[i];
        }
    }
    return temp;
}

template<> char* const maxn( char* const arr[],int n)
{
     char* temp = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (strlen(arr[i])>strlen(temp))
        {
            temp = arr[i];
        }
    }
    return temp;
}
刚开始我和题主一样错误,百思不得其解,我用的visual studio 2015,把鼠标放在函数调用的位置,正确的写法就出来啦
1