惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园_首页
博客园 - 【当耐特】
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 叶小钗
S
SegmentFault 最新的问题
雷峰网
雷峰网
H
Help Net Security
宝玉的分享
宝玉的分享
A
About on SuperTechFans
IT之家
IT之家
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) Codeforces Round 907 (Div. 2) Codeforces Round 916 (Div. 3)
C++自定义的字面量
Shiroha · 2024-01-01 · via Shiroha白羽的博客

最近在看 CPP 的一些东西的时候,发现了一个非常有意思的特性。不得不说,CPP 的特性总是朝着一些奇奇怪怪的方向发展,整出了一堆奇奇怪怪的语法糖

这个特性叫 User-defined literals

非常简单的一种特性,例如很多时候我们会用一些结构体来表示内存/磁盘等大小,比如这种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static size_t B = 1;
static size_t KB = 1000 * B;
static size_t MB = 1000 * KB;
static size_t GB = 1000 * MB;
static size_t KiB = 1024 * B;
static size_t MiB = 1024 * KiB;
static size_t GiB = 1024 * MiB;

struct Size {
size_t value;
};

inline std::ostream& operator<<(std::ostream& o, const Size& size) {
if (size.value < KiB) o << size.value << 'B';
// NOLINTNEXTLINE(*-narrowing-conversions)
else if (size.value < MiB)o << std::fixed << std::setprecision(2) << 1.0 * size.value / KiB << "KiB";
// NOLINTNEXTLINE(*-narrowing-conversions)
else if (size.value < GiB)o << std::fixed << std::setprecision(2) << 1.0 * size.value / MiB << "MiB";
// NOLINTNEXTLINE(*-narrowing-conversions)
else o << std::fixed << std::setprecision(2) << 1.0 * size.value / GiB << "GiB";
return o;
}

这样就可以有一个表示占用大小的类型了,但是这样并不方便,因为初始化的时候还需要明确表示这个类型。但是用上这个特性之后就可以非常简单,只需要再定义几个方法

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
inline Size operator ""_B(unsigned long long v) {
return {v * B};
}

#pragma region 1000

inline Size operator ""_KB(const unsigned long long v) {
return {v * KB};
}

inline Size operator ""_MB(const unsigned long long v) {
return {v * MB};
}

inline Size operator ""_GB(const unsigned long long v) {
return {v * GB};
}

#pragma endregion

#pragma region 1024

inline Size operator ""_KiB(const unsigned long long v) {
return {v * KiB};
}

inline Size operator ""_MiB(const unsigned long long v) {
return {v * MiB};
}

inline Size operator ""_GiB(const unsigned long long v) {
return {v * GiB};
}

#pragma endregion

这样之后就可以直接用字面量初始化

1
2
const auto v = 123435245_KiB;
std::cout << v << std::endl;