Wednesday, August 16, 2017

C++11: C99 Features: _Pragma

A functional form of pragma was added. _Pragma( X ) expands to #pragma X
This does not work on Microsoft Visual Studio 2015.  You need to replace _Pragma with __pragma


C++11: C99 Features: __func__

The macro __func__ was added, so that you could get the string of the current function. This is useful for trace statements.


C++11: Alignment

You can use the alignof() function to get the alignment (in number of bytes) of a type. Here is an example:
constexpr int intAlignmentInBytes = alignof(long long);


C++11: Alignment

You can specify the alignment of arrays with the alignas() operator. Previously, if you allocated bytes, you could not guarantee the alignment, which caused exceptions when the bytes were used as raw memory to back larger data types. Here is an example:
alignas(double) unsigned char buffer[1024];


C++11: Attributes

Attributes where added to C++11. They are contained within double square brackets: [[…]]. The double brackets can have spaces between them. The attributes come right after the entity they modify.  The attributes can be future C++ options, or vendor-specific options.


C++11: Raw String Literals

If you have a lot of characters that need to be escaped, you can use raw string literals to simplify a string. Here is an example:

#include <iostream>
int main()
{
  std::cout << R"(\\\\\)" << std::endl;
  return 0;
}
// Output: \\\\\
Reference: https://isocpp.org/wiki/faq/cpp11-language-misc#raw-strings

C++11: Static Assertion


static_assert(<bool_constexpr> , <message>); was added to c++11.
Here is an example:

static_assert( (sizeof(long) == 3), "Error: the type int is not 3 bytes");
int main() {return 0;}
// Output During Compile: error C2338: Error: the type int is not 3 bytes

Reference: https://isocpp.org/wiki/faq/cpp11-language-misc#static-assert
                  http://en.cppreference.com/w/cpp/language/static_assert

C++11: Local Types as Template Arguments

Local and Unnamed types can now be used as template arguments.


C++11: Variadic Templates

Variadic Parameters were added to Templates.

#include <iostream>

template<class ... Ts>
void f(Ts ... args)
{
  const int size = sizeof...(args) + 1;
  int res[size] = {1, args...};

  std::cout << "------" << std::endl;
  for (int i = 1; i < size; i++)
  {
    std::cout << res[i] << std::endl;
  }
}

int main()
{
  f();
  f(1);
  f(1, 2);
  f(1, 2, 3);
  return 0;
}
/* Output:
------
------
1
------
1
2
------
1
2
3
*******/

References: https://isocpp.org/wiki/faq/cpp11-language-templates#variadic-templates
                    http://en.cppreference.com/w/cpp/language/parameter_pack

C++11: Template Aliases

You can create Template that renames another template, but with some template parameters filled in. Here is an example:

template<class T, class U, class V>
struct MyTemplate {T t; U u; V v;};

template<class T>
using MyTemplateUV =  MyTemplate<T, int, float>;

MyTemplateUV<int>           s = {1, 2, 3.0};
MyTemplate  <int,int,float> t = {4, 5, 6.0};

int main()
{
  t = s;
  return 0;
}

Reference: https://isocpp.org/wiki/faq/cpp11-language-templates#template-alias

Monday, August 7, 2017

C++11: Suffix Return Type Syntax


The following function:
      int function();
Can also be written like:
      auto function() -> int;
Reference: https://isocpp.org/wiki/faq/cpp11-language-misc#suffix-return

C++11: Right-angle Brackets


list<vector<string>> lvs; works
Reference: https://isocpp.org/wiki/faq/cpp11-language-misc#right-angles

C++11: Preventing narrowing

{} initialization doesn’t narrow

C++11: The value of __cplusplus

The value of __cplusplus for C++11 is 20113L.

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