显式专业化:语法错误? [英] explicit specialization: syntax error?

查看:91
本文介绍了显式专业化:语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它需要一个模板函数,其中包含一系列项目以及该数组中的项目数量作为参数。该函数需要返回所述数组中的最大项目。该程序还应该有一个专门化(我有问题),检查数组中最长的字符串,如果输入了一个字符串数组。



以下是我的原型:

  template< typename T> 
T比较(T const arr1 [],int n);

模板<> const char * compare< const char *>(const char * const arr2 [] [3],int n);

主程序。

  int main()
{
int a [7] = {3,67,100,91,56,67,83};
double b [] = {2.5,2.6102,2.61,1.03};
// char * c [3] = {看看它是否有效,功能,明确的专业化};
char c0 [30] = {看看这是否有效};
char c1 [30] = {functions};
char c2 [30] = {显式专业化};
char * d [] [3] = {c0,c1,c2};

cout<< 比较int值:<< ENDL;
cout<< 最大的int值是:<<比较(a,7)<< ENDL;
cout<< 比较双倍值:<< ENDL;
cout<< 最大的双重价值是:<<比较(b,4)< ENDL;

返回0;

函数定义...

 模板< typename T> 
T比较(T const arr1 [],int n){
T temp;
........
return temp;
}

模板<> const char * compare< const char *>(const char * const arr2 [] [3],int n); {
const char * temp2 =& arr2 [0];
return * temp2;
}

我写了一些虚拟代码来测试第二个函数是否工作,但它不返回正确的值(它返回'明确的特殊化')有人可以请指出我的语法有什么问题吗?

解决方案

如上所述,与专门化相比,重载更适合于这个问题:

  const char * compare(const char * const arr2 [],int n); 

请注意,尽管我将方括号放入参数类型以匹配模板,但此声明是等效的到 const char * const * arr2 ,因为函数参数在这方面是特殊的。




假设你绝对需要专门化(尽管解释也适用于上述解决方案):



考虑 T 是你的专长。 T 是序列的元素类型,您已经为 T = char 指定了模板。这意味着你已经为你的模板专门化了一系列字符,而不是一串字符串。



为了专门处理一系列C字符串,用 const char * 用于 T 而不是 char

 模板<> const char * compare< const char *>(const char * const arr2 [],int n); 


I am writing a program that requires a template function having an array of items, and number of items in that array as arguments. The function needs to return the largest item in said array. The program should also have a specialization (what I'm having issues with) which checks for the longest string in the array, if an array of strings is entered.

Here are my prototypes:

template <typename T>
T compare(T const arr1[], int n);

template <> const char *compare<const char *>(const char *const arr2[][3], int n);

main program..

int main()
{
    int a[7] = { 3, 67, 100, 91, 56, 67, 83 };
    double b[] = { 2.5, 2.6102, 2.61, 1.03 };
    //char* c[3] = { "see if this works", "functions", "explicit specialization" };
    char c0[30] = { "see if this works" };
    char c1[30] = { "functions" };
    char c2[30] = { "explicit specialization" };
    char *d[][3] = { c0, c1, c2 };

    cout << "Comparing int values: " << endl;
    cout << "Largest int value is: " << compare(a, 7) << endl;
    cout << "Comparing double values: " << endl;
    cout << "Largest double value is: " << compare(b, 4) << endl;

    return 0;
}

function definition...

template <typename T>
T compare(T const arr1[], int n) {
    T temp;
........
    return temp;
}

template <> const char *compare<const char *>(const char *const arr2[][3], int n); {
    const char *temp2 = &arr2[0];
    return *temp2;
}

I wrote some dummy code to test whether the second function works but it does not return the correct value (it returns 'explicit specialization') Can someone please point out to me what's wrong with the syntax?

解决方案

As said above, an overload is much more suited to this problem than a specialization:

const char *compare(const char *const arr2[], int n);

Note that while I put the square brackets into the parameter type to match the template, this declaration is equivalent to one with const char *const *arr2 because function parameters are special in this regard.


Assuming you absolutely require a specialization for whatever reason (although the explanation can also apply to the above solution):

Consider what T is and what your specialization is for. T is the element type of the sequence and you've specialized your template for T=char. This means that you've specialized your template for a sequence of characters, not a sequence of strings.

To specialize for a sequence of C strings, substitute const char * for T rather than char:

template <> const char *compare<const char *>(const char *const arr2[], int n);

这篇关于显式专业化:语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆