C++核心准则​CPL.1:C++比C更好​

时间:2022-07-26
本文章向大家介绍C++核心准则​CPL.1:C++比C更好​,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

CPL.1: Prefer C++ to C

CPL.1:C++比C更好

Reason(原因)

C++ provides better type checking and more notational support. It provides better support for high-level programming and often generates faster code.

C++提供更好的类型检查和更多的记法支持。它为高层次编程提供更好的支持,通常也会产生更快的代码。

Example(示例)

char ch = 7;
void* pv = &ch;
int* pi = pv;   // not C++
*pi = 999;      // overwrite sizeof(int) bytes near &ch

The rules for implicit casting to and from void* in C are subtle and unenforced. In particular, this example violates a rule against converting to a type with stricter alignment.

C语言中有关void*的隐式类型转换的规则含糊且无用。更加严重的是,这个例子违反了防止使用直接的赋值实现类型转换的规则。

Enforcement(实施建议)

Use a C++ compiler.

使用C++编译器。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cpl1-prefer-c-to-c