C++11, BS book extract, part II, chapter 6

Fundamental Types

  • bool
  • (signed/unsigned)char, wchar_t, char16_t (UTF16), char32_t(UTF32)
  • short, (signed/unsigned) int, long long
  • float, double, long double
  • void
  • pointer
  • array
  • reference
  • struct, class
  • enum

{} -initializer syntax to prevent narrowing alignof() operator returns the alignment of its argument expression we can use the type specifier alignas : alignas(T) means ‘‘align just like a T .’’ void user(const vector& vx) {     constexpr int bufmax = 1024;     alignas(X) buffer[bufmax];     // uninitialized     const int max = min(vx.size(),bufmax/siz eof(X));     uninitialized_copy(vx.begin(),vx.begin()+max,buffer);     // … }   Scope

  • Local scope
  • Class scope
  • Namespace scope
  • Global scope
  • Statement scope
  • Function scope

Initialization X a1 {v}; – initializer list – prevents data gets lost, like narrowing (integer of float point datatypes) or floating integer type conversion but auto should be initialized with assignment auto z1 {99}; // z1 is an initializer_list auto z2 = 99; // z2 is an int difference for vector as well vector v1 {99}; // v1 is a vector of 1 element with the value 99 vector v2(99); // v2 is a vector of 99 elements each with the default value 0  Prefer {} initialization over alternatives unless you have a strong reason not to. The empty initializer list, {} , is used to indicate that a default value is desired. Nice example – char buf[max] {}; // initialize every char to 0 If initialization is missed

  • Static objects (global, local static, static members) are initialized by default
  • Dynamic – heap or stack based – are not initialized.

The language provides two mechanisms for deducing a type from an expression:

  • auto for deducing a type of an object from its initializer; the type can be the type of a vari- able, a const , or a constexpr .
  • decltype(expr) for deducing the type of something that is not a simple initializer, such as the return type for a function or the type of a class member.

Weird syntax template auto operator+(const Matrix& a, const Matrix& b) −> Matrix<decltype(T{}+U{})>;   The lifetime of an object starts when its constructor completes and ends when its destructor starts executing.

  • Automatic
  • Static
  • Free store (Heap)
  • Temporary objects
  • Thread-local objects; that is, objects declared thread_local

using instead of typedef, using could not be prepended with unsigned/signed


About DmitryKrinitsyn
Software developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: