实时 C++ 快速入门:实用特性与技巧
1. 代码片段引入
在 C++ 编程中,有一个代码片段展示了一些实用的特性:
point<std::int16_t, std::int32_t> p
{
INT16_C(-2129),
INT16_C(+5471)
};
const std::int32_t d = p.squared_euclidean_distance();
// d is 34,464,482
此代码使用了编译时断言( static_assert )结合 <type_traits> 库,确保模板参数为整数类型。
2. nullptr 替代 NULL
自 C++11 起,C++ 引入了 nullptr 关键字,它消除了对 NULL 冗余且可能冲突的定义,避免了手写零指针测试的麻烦。以下是使用 nullptr 测试指针是否为非零的示例:
// chapter03_07-001_nullptr.cpp
class something
{
public:
something() { }
};
namespace
{
// Default initialized to nullptr (i.e., 0).
something* p
超级会员免费看
订阅专栏 解锁全文

22万+

被折叠的 条评论
为什么被折叠?



