Type System (Basics)

Type Aliases

using NewType = OldType; C++11

typedef OldType NewType; C++98

using real = double;
using ullim = std::numeric_limits<unsigned long>;
using index_vector = std::vector<std::uint_least64_t>;
  • Prefer the more powerful (we’ll later see why) using over the outdated and ambiguous typedef!

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                 
auto u = 56u; // unsigned int
auto d = 2.023; // double
auto f = 4.01f; // float
auto l = -78787879797878l; // long int
auto x = 0 * i; // int
auto y = i + d; // double
auto z = f * d; // double

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 be constexpr themselves
  • constexpr functions may contain
    • C++11  nothing but one return statement
    • C++14  multiple statements
// two simple functions:
constexpr int cxf (int i) { return i*2; }
int foo (int i) { return i*2; }
constexpr int i = 2; // OK '2' is a literal
constexpr int j = cxf(5); // OK, cxf is constexpr
constexpr int k = cxf(i); // OK, cxf and i are constexpr
int x = 0; // not constexpr
int l = cxf(x); // OK, not a constexpr context
// constexpr contexts:
constexpr int m = cxf( x ); // WRONG
constexpr int n = foo( 5 ); // WRONG