Modern C++ Learning(4)-Type System (Basics)
Type System (Basics)
Type Aliases
using NewType = OldType; C++11
typedef OldType NewType;
C++98
using real = double; |
- Prefer the more powerful (we’ll later see why)
using
over the outdated and ambiguoustypedef
!
Type Deduction: auto C++11
auto variable = expression;
- variable type deduced from right hand side of assignment
- often more convenient, safer and future proof
- also important for generic (type independent) programming
auto i = 2; // int |
Constant Expressions: constexpr C++11
- must be computable at compile time
- can be computed at runtime if not invoked in a
constexpr
context - all expressions inside a
constexpr
context must beconstexpr
themselves - constexpr functions may contain
C++11
nothing but one return statementC++14
multiple statements
// two simple functions: |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 王赵安的博客!
评论