uacs2024

导航

模板特化与unsigned char

模板特化与unsigned char

#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]";
    char str2[] = "Trendmicro[char2]";
    unsigned char ustr[] = "Trendmicro[unsigned char]";
    print(str);
    print(str2);
    print(ustr);
    return 0;
}

结果

The string is Trendmicro[char]
The string is Trendmicro[char2]
The value is Trendmicro[unsigned char]

特化通常指的是模板特化,即为已有的模板参数进行特殊化的指定,使得以前不受任何约束的模板参数受到特定的修饰或完全被指定下来。模板特化分为函数模板的特化和类模板的特化,又可以根据特化范围的不同分为全特化和偏特化。

第一个是 char *  有恰好匹配的函数。unsigned char不是char,第二个没有就找模板。

char 与 unsigned char区别

1.char和unsigned char 都是一个byte,8个bit。char是无符号类型,首位bit是符号位。

2.取值范围不同:

(1)unsigned char的取值范围:0~2^8-1(0~255)

(2)char的取值范围:-2^7~2^7-1(-128~127)

posted on 2024-03-15 14:26  ᶜʸᵃⁿ  阅读(13)  评论(0编辑  收藏  举报