UTF编码内存角度比较.md

UTF-8、UTF-16 和 UTF-32 是三种不同的 Unicode 编码方式,它们在表示字符时占用的字节数各不相同。具体如下:

  1. UTF-8
    • UTF-8 是一种可变长度的编码方式,每个字符占用 1 到 4 个字节。
    • 具体字节数取决于字符的 Unicode 码点:
      • U+0000 至 U+007F(基本拉丁字母)占 1 个字节。
      • U+0080 至 U+07FF 占 2 个字节。
      • U+0800 至 U+FFFF 占 3 个字节。
      • U+10000 至 U+10FFFF 占 4 个字节。

阅读全文

tag dispatch

对于short类型来说,会优先匹配通用引用版本的重载,导致无法构造string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
namespace TagDispatch {

template <typename T>
void fun(T&& params) {
std::vector<std::string> temp;
temp.emplace_back(std::forward<T>(params));
std::cout << "called fun(T&& params)" << std::endl;
}

void fun(int a) {
std::cout << "called fun(int a)" << std::endl;
}

template <typename T>
void fwd(T&& params) {
fun(std::forward<T>(params));
}

void test()
{
fwd("abc"); //suc
int a = 0;
fwd(a); //suc

short b = 10;
//fwd(b); //failed, 匹配到了通用引用函数

}

int main(int argc, const char * argv[]) {
TagDispatch::test();

return 0;
}

}

阅读全文

模板规则推导

模板推导规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
template<typename T>
void passByRefFun(T& val) { }

template<typename T>
void passByUniRefFun(T&& val) { }

template<typename T>
void passByValueFun(T val) { }

void fun3(int& a) { }
void fun3(int&& a) { }

void test()
{
int a = 0; //a int
const int b = a; //b const int
const int& c = a; //c const int&

passByRefFun(a); // int&
passByRefFun(b); // const int&
passByRefFun(c); // const int&
//passByRefFun(27); // 报错

passByUniRefFun(a); // int&
passByUniRefFun(b); // const int&
passByUniRefFun(c); // const int&
passByUniRefFun(27); // int&&

passByValueFun(a); // int
passByValueFun(b); // int
passByValueFun(c); // int
passByValueFun(27); // int

{
int&& a = 10;
fun3(a); // called void fun3(int& a)
fun3(std::move(a)); // called void fun3(int&& a)

}
}

阅读全文

c++继承权限

  1. 继承有三种权限,public,proteced,private,默认不写是private

  2. 权限的最低是public,其次是protected,最高private

  3. 继承方式代表是父类属性在当前类中的最低呈现

  4. 父类中的privated属性在子类中不可访问