Modern C++ Learning(1)-Fundamental Types
Fundamental Types
Variable Declarations
c++
type variable = value; |
Quick Overview
Signed Integers
c++
long l1 = -7856974990L; |
Unsigned Integers
c++
unsigned u1 = 12347U; |
Floating Point Types
c++
float f = 1.88f; |
Comparisons
3-Way Comparisons With <=> C++20
c++
determines the relative ordering of 2 objects: |
Memory Sizes of Fundamental Types
Integer Size Guarantees C++11
c++
exact size (not available on some platforms)
int8_t, int16_t, int32_t, int64_t, uint8_t, …
guaranteed minimum size
int_least8_t, uint_least8_t, …
fastest with guaranteed minimum size
int_fast8_t, uint_fast8_t, …
Fixed_Width Floating Point Type Guarantees C++23
c++
|
Type Narrowing
- conversion from type that can represent more values to one that can represent less
- may result in loss of information
- in general no compiler warning – happens silently
- potential source of subtle runtime bugs
c++
double d = 1.23456; |
Braced Initialization C++11
c++
type variable { value }; |
- works for all fundamental types
- narrowing conversion ⇒ compiler warning
c++
double d {1.23456}; // OK |
References
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 王赵安的博客!
评论