Effective Modern C++
用static_cast<int>()
而非(int)
这是新写法。如:
float floatPi = 3.14f;
int intPi = static_cast<int>(floatPi);
与之类似的还有:dynamic_cast
、const_cast
、reinterpret_cast
用using
而非typedef
这也是新写法。如:
using VI = std::vector<int>;
//而不是 typedef std::vector<int> VI;
VI vi;
不要using namespace std
之所以要设置std
这个命名空间,就是为了防止重名
而using namespace std
则将这一努力毁之一旦
如果输入输出要打std::
太麻烦,建议这样做:
using std::cin;
using std::cout;
位域
这个看上去很不错。
Lnk
拒绝enum
,改用enum class
- 命名空间不重合
- 防止意外转换
- …
右值引用、移动语义和完美转发
可以说是Effective Modern C++关于这一块的整理。
C++14引入std::quoted()
用于给字符串添加双引号
std::quoted("a");
--> "\"a\""