Monday, August 7, 2017

C++11: Extern templates

Templates can be declared as extern, so that they are redundantly specialized.

C++11: PODs (Plain Old Data)

POD data lets you interoperate with C. The binary layout are compatible. C++11 PODs do not matter on whether or not you have constructors.

C++11: Unions: Members with CTORs and DTORs

Unions can now allow members with CTORs and DTORs. They are marked deleted by the compiler.

C++11: long long

A long long type is an integer that is at least 64-bits long. A long type can be less than 64-bits.

C++11: enum class: Underlying Type

You can specify the underlying type of an enum class. Here is an example:

#include <iostream>
enum       EnumDefault                   {A, B};
enum       EnumChar          : char      {C, D};
enum       EnumShort         : short     {E, F};
enum       EnumInt           : int       {G, H};
enum       EnumLongLong      : long long {I, J};
 
enum class EnumClassDefault              {A, B};
enum class EnumClassChar     : char      {C, D};
enum class EnumClassShort    : short     {E, F};
enum class EnumClassInt      : int       {G, H};
enum class EnumClassLongLong : long long {I, J};
 
int main()
{
  std::cout << sizeof(EnumDefault      ) << " " <<
               sizeof(EnumChar         ) << " " <<
               sizeof(EnumShort        ) << " " <<
               sizeof(EnumInt          ) << " " <<
               sizeof(EnumLongLong     ) << " " <<
 
               sizeof(EnumClassDefault ) << " " <<
               sizeof(EnumClassChar    ) << " " <<
               sizeof(EnumClassShort   ) << " " <<
               sizeof(EnumClassInt     ) << " " <<
               sizeof(EnumClassLongLong) << std::endl;
  return 0;
}
// Output: 4 12 4 8 4 1 2 4 8
Reference: https://isocpp.org/wiki/faq/cpp11-language-types#enum-class

C++11: enum class

enum classes do not get implicitly converted to an integer. enum classes are scoped. Here is an example:

enum Alpha {A, B, C};
enum class Beta {X, Y, Z};
int main()
{
    Alpha alpha = A;
    Beta  beta  = Beta::X;
    int   alpha_int = alpha;
    int   beta_int  = static_cast(beta); // Cast is necessary in order to compile.
    return 0;
}
Reference: https://isocpp.org/wiki/faq/cpp11-language-types#enum-class