Skip to content

Commit f6935e6

Browse files
authored
Update cpp_tricks.md
1 parent 7419b95 commit f6935e6

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

docs/cpp_tricks.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ struct Child : Parent {
343343
Child child(23, "peng"); // 错误!Child 没有构造函数!
344344
```
345345

346-
可以在子类里面写 `using 父类::父类`,就能自动继承父类所有的构造函数了。
346+
C++11 中可以在子类里面写 `using 父类::父类`,就能自动继承父类所有的构造函数了。
347347

348348
```cpp
349349
struct Parent {
@@ -2038,7 +2038,14 @@ TODO
20382038

20392039
## requires 语法检测是否存在指定成员函数
20402040

2041-
## 设置 locale 为 .utf8 解决编码问题
2041+
## 设置 locale 为 .utf8 解决 Windows 编码难问题
2042+
2043+
```cpp
2044+
system("chcp 65001");
2045+
setlocale("LC_ALL", ".utf-8");
2046+
```
2047+
2048+
> {{ icon.tip }} 详见 [Unicode 专题章节](unicode.md)。
20422049
20432050
## 成员函数针对 this 的移动重载
20442051
@@ -2082,3 +2089,42 @@ int b = f >> 4; // 0x2
20822089
## swap 缩小 mutex 区间代价
20832090

20842091
## namespace 别名
2092+
2093+
有些嵌套很深的名字空间每次都要复读非常啰嗦。
2094+
2095+
```cpp
2096+
#include <filesystem>
2097+
2098+
int main() {
2099+
std::filesystem::path p = "/var/www/html";
2100+
...
2101+
}
2102+
```
2103+
2104+
如果 `using namespace` 的话,又觉得污染全局名字空间了。
2105+
2106+
```cpp
2107+
#include <filesystem>
2108+
2109+
using namespace std::filesystem;
2110+
2111+
int main() {
2112+
std::filesystem::path p = "/var/www/html";
2113+
...
2114+
}
2115+
```
2116+
2117+
可以用 C++11 的 `namespace =` 语法,给名字空间取个别名。
2118+
2119+
```cpp
2120+
#include <filesystem>
2121+
2122+
namespace fs = std::filesystem;
2123+
2124+
int main() {
2125+
fs::path p = "/var/www/html";
2126+
...
2127+
}
2128+
```
2129+
2130+
这样以后就可以 `fs` 这个简称访问了。

0 commit comments

Comments
 (0)