首页 > 试题广场 >

以下程序的输出是: #include ...

[单选题]

以下程序的输出是:

#include <iostream>
using namespace std;
template <typename T>
void print(T t){
    cout<<"The value is "<<t<<endl;
}
template <>
void print<char *>(char* c){
    cout<<"The string is " << c <<endl;
}
int main(){
    char str[] = "TrendMicro[char]";
    unsigned char ustr[] = "TrendMicro[unsigned char]";
    print(str);
    print(ustr);
    return 0;
}

  • The value is TrendMicro[char]<br>The string is TrendMicro[unsigned char]
  • The value is TrendMicro[char]<br>The value is TrendMicro[unsigned char]
  • The string is TrendMicro[char]<br>The string is TrendMicro[unsigned char]
  • The string is TrendMicro[char]<br>The value is TrendMicro[unsigned char]
答案是d,本题有错误
发表于 2020-08-17 11:33:49 回复(0)
这道题答案是有误的哦,模板也是有优先级的,特例化模板优先匹配特例化模板  
发表于 2020-09-05 11:28:34 回复(1)
特例化模板指:优先匹配类型完全相同的模板
发表于 2021-03-31 11:04:26 回复(0)
str传入时为数组首地址char*,ustr为unsigned char*
发表于 2022-04-02 19:07:59 回复(0)
str 是 char* 类型,调用的是模板特化版本 print<char *>(char* c)
ustr 是 unsigned char* 类型,调用的是通用模板版本 print<T>(T t)
发表于 2024-11-13 14:05:38 回复(0)
ustr不是指针吗
发表于 2021-10-29 19:10:51 回复(0)
str数组传参退化成指针,函数匹配时存在特例化模板优先匹配特例化模板,精确匹配的话不再查找,有大佬指点一下这个思路哪里有问题或者漏洞吗?
发表于 2020-08-12 15:19:41 回复(0)
优先匹配类型相同的模板,找不到就用通用模板
发表于 2024-12-25 19:42:46 回复(0)
发表于 2020-08-29 10:51:47 回复(0)
VS2019 验证选D,先精确匹配已经实例化的模板
发表于 2020-08-22 09:51:53 回复(1)
请求大佬解答
发表于 2020-08-16 21:31:58 回复(0)