Saturday, November 12, 2011

Predefined variable __func__ in C++11

In C++11, every function has a local predefined variable __func__ that looks as follows:

static const char __func__[] = "function-name";
This variable can for example be used for logging purposes.

Range based for loop in C++11

Range based for loop allow easy iteration through list of elements. This type of loop will work for C-style arrays, initializer lists and any type that has a begin() and end() function that returns iterators.
In the below example, the range-based for loop will then iterate over every element in this list and increment its value by 2.
int arr[] = {1,2,3,5};
for(auto &i : arr) {
    i += 2;
} 

std::vector<int> myVector (arr, arr+4);
for (auto j: myVector)
   std::cout << j<< std::endl;
To support the range-for loop, the user defined types (classes) must have suitable begin() and end() functions.

Tuesday, November 8, 2011

Strongly typed enum in C++11

C++11 introduces the enum class which are strongly typed means, they are type-safe. Enum's are interpreted as integers and thus you can compare enumeration values from completely different enumeration types.

enum class MyEnum
{
VALUE1,
VALUE2 = 10,
VALUE3
};
So this is not legal in C++11:
if (MyEnum::VALUE3 == 11) {...}

By default, the underlying type of an enumeration value is an integer, but this can be changed as follows:


enum class MyEnumLong : unsigned long
{
LVALUE1,
LVALUE2 = 10,
LVALUE3
};
Enum class type is user defined so we can define operators. By default Enum class has only assignment, initialization, and comparisons operator.
enum class color { green, yellow, red };
color& operator++(color& t)
// prefix increment: ++
{
switch (t) {
case color::green: return t = color::yellow;
case color::yellow: return t = color::red;
case color::red: return t = color::green;
}
}
color next = ++light; // next becomes color::green

Saturday, November 5, 2011

Use of namespace in c++

From Professional C++ 2nd edition:
Namespaces address the problem of naming conflicts between different pieces of code.
#include "namespaces.h"
#include <iostream>
namespace mycode {
    void foo() {
        std::cout << "foo() called in the mycode namespace" << std::endl;
    }
}
To call the namespace-enabled version of foo(), prepend the namespace onto the function name by using :: also called the scope resolution operator as follows.
mycode::foo();    // Calls the "foo" function in the "mycode" namespace

You can also avoid prepending of namespaces with the using directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace.

A single source file can contain multiple using directives, but beware of overusing this shortcut. In the extreme case, if you declare that you’re using every namespace known to humanity, you’re effectively eliminating namespaces entirely!

The using directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows:
using std::cout;

Subsequent code can refer to cout without prepending the namespace, but other items in the std namespace will still need to be explicit:
using std::cout;

cout << "Hello, World!" << std::endl;