C Primier Plus --chapter two

Top-Level const

A pointer who is const is top-level const,
and the object it point to is const ,it is low-level
pointer must point to an object,not a value.
pointer pointe to an const object may not be initialize,but const pointer must be intialized when defined.

  • Top-level indicates that an object itself is const, and appear in any object type.
  • Low-level const only appear in the base type of compiund type such as pointer and reference
  • when copy an object top-level const will be ipnored
  • when copy an object low-level const must be the same ,because it cann’t be ignored

Constexpr

  1. constexpr : a expression whose value can nor chage and can be evaluated at compile time
  2. a constant expression is one that can be evaluated at compile time
  3. define a pointer in a constexprdeclaration, the constexpr specifier applies to the pointer, not the type to which the pointer points
1
2
$ const int *p = nullptr; p is a pointer to a const int
$ constexpr int *q = nullptr; q is a const pointer to int

constexpr impose a top-level const on the object it defines


2.5Type

Typedef type alias : a synonymm for another type.
simplyfy complicated type definitions,emphasize the purpose for which a type ia used.
An alias by c++11

1
using SI = Sales_item; // SI is a synonym for Sales_item

auto

  1. auto let the compiler to deduce the type from the initializer
  2. auto adjusts the type to conform to normal initialization rules.
    auto sz = 0, pi = 3.14; the compiler cann`t deduce the type of the two variable.
  3. when we use a reference or pointer as a initializer,
    we are using the object to which it refers or pointe.
    int i = 0, &r = i;
    3.auto orfinarily ignores top-level consts.
    auto a = r; // a is an int
    if want to deduce type have a top-level *const*,you must say explicitlly. const auto

decltype :return the type of its operand.
The compiler analyzes the expression to determine its type but does not evaluate the expression

1
decltype(f()) sum = x; // sum has whatever type f returns

  • If we wrap the variable’s name in one
    or more sets of parentheses, the compiler will evaluate the operand as an expression

    Assignment is an example of an expression that yields a reference type.
1
2
3
int a = 3, b = 4;
decltype(a = b) d = a;
d is a reference of int. the value: d=3

  1. decltype returns the type of that variable, including top-level const and references,which auto ignors.
  2. decltype and auto is that the deduction done by decltype depends on the form of its given expression.