Type system
C++在C的基础上对type system做了很多改进,使得相比于C而言,C++有着更好的type safety,主要是通过:
1、充分发挥static type实现的
NOTE: 可以作为static type VS dynamic type的素材。
CppCoreGuidelines Philosophy
P.4: Ideally, a program should be statically type safe
P.5: Prefer compile-time checking to run-time checking
2、执行更加严格的类型检查
在wikipedia Type safety 中对此进行了非常好的总结:
wikipedia Type safety
Some features of C++ that promote more type-safe code:
1、The new operator returns a pointer of type based on operand, whereas malloc returns a void pointer.
2、C++ code can use virtual functions and templates to achieve polymorphism without void pointers.
NOTE:
1、void pointer 是典型的type erasure,关于void pointer 和 template的对比,参见
C++\Programming-paradigm\Type-erasure-OOP-GP
,其中的ixodoi.expertscolumn-C++ Programming-void-pointer-VS-template
对此进行了非常深入的分析
3、Preprocessor constants (without type) can be rewritten as const variables (typed).
4、Preprocessor macro functions (without type) can be rewritten as inline functions (typed). The flexibility of accepting and returning different types can still be obtained by function overloading.
5、Safer casting operators, such as dynamic cast that performs run-time type checking.
Effective type
c中有effective type的概念,而c++
中则没有;
C++
中并没有effective type的概念;其实主要原因在于c++中使用了new
,而c中则是malloc
,显然这是c++
在type safety上的一些改善,这一点在wikipedia Type safety中有解释;
new
和malloc
这在 Resource-management\Memory-management\Allocation-and-deallocation\VS-malloc-VS-new
章节进行了介绍。
CV
C++在CV方面,比C要严格:
1、string literal:c中的类型char *
,c++中是const char *
2、C-style cast会drop掉CV,C++的各种type cast函数如reinterpret_cast
、static_cast
等,都需要保持CV,C++中需要通过const_cast
来drop掉CV,相比于C的implicit,C++的是explicit,这一点,在C++\Language-reference\Basic-concept\Type-system\Type-conversion\reinterpret_cast.md#CV
段中进行了详细介绍
Type conversion
关于这一点,在C++\Language-reference\Basic-concept\Type-system\Type-conversion\Cast-operator\index.md
中进行了详细比较。
Type punning
关于这一点,在C++\Language-reference\Basic-concept\Type-system\Type-punning
中进行了详细比较。
std::variant
VS union
C++17引入的std::variant
是对union的改进,关于此,参见C++\Library\Standard-library\Utility-library\Common-vocabulary-types
。
std::any
VS void*
C++17引入的std::any
是对void*
的改进,关于此,参见C++\Library\Standard-library\Utility-library\Common-vocabulary-types
。
Variadic template VS ellipsis
参见 C++\Language-reference\Functions\Generic-programming\Variadic-function
章节。
Void pointer conversion
参见 :
1、thegreenplace-void-pointer-and-casts-in-C-and-C++