Wednesday, 13 June 2018

C++ 11 features

1- auto
C++11 introduces  type inference capability using the auto keyword.
auto p = s.begin();
auto n = 10;

nullptr
The constant 0 has had the double role of constant integer and null pointer constant
ptr2 = 0; // ptr2 is now a null pointe
int *ptr { nullptr };

- shared_ptr
Smart pointer is not a new concept, many libraries implemented it many years ago, the popular one is boost::shared_ptr, what’s new is its  standardization, and no need anymore to use an external library to work with smart pointers.
 Range-based for loops
C++11 augmented the for statement to support the “foreach” paradigm of iterating over collections.
int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
    for (int number : fibonacci) // iterate over array fibonacci
       std::cout << number << ' '; // we access the array element for this iteration through variable number

R-value references
1
2
3
int x = 5;
int &lref = x; // l-value reference initialized with l-value x
int &&rref = 5; // r-value reference initialized with r-value 5


 Unordered containers
A unordered container is a kind of hash table. C++11 offers four standard ones:

are immutable i.e. they can not be modified once inserted

  • unordered_map
  • unordered_set
  • unordered_multimap
  • unordered_multiset




override specifier

override is an identifier with a special meaning when used after member function declarators: it's not a reserved keyword otherwise.
struct A
{
    virtual void foo();
    void bar();
};
 
struct B : A
{
    void foo() const override; // Error: B::foo does not override A::foo
                               // (signature mismatch)
    void foo() override; // OK: B::foo overrides A::foo
    void bar() override; // Error: A::bar is not virtual
};


Initilisation list
Point(int i = 0, int j = 0):x(i), y(j) {}
    /*  The above use of Initializer list is optional as the
        constructor can also be written as:
        Point(int i = 0, int j = 0) {
            x = i;
            y = j;
        }
    */   


 initialization of reference members:
class Test {
    int &t;
public:
    Test(int &t):t(t) {}  //Initializer list must be used


Initialization of base class member
B::B(int x):A(x) { //Initializer list must be used
-------------------------------
std::thread is the thread class that represents a single thread in C++.
import<thread>
std::thread thread_object(callable)
Launching thread using function pointer
The following code snippet demonstrates how this is done


void foo(param)
{
    // Do something
}
 
// The paramters to the function are put after the comma
std::thread thread_obj(foo, params);
Launching threads using function objects
The following code snippet demonstrates how this is done
// Define the class of function object
class fn_object_class {
    // Overload () operator
    void operator()(params)
    {
        // Do Something
    }
}
 
// Create thread object
std::thread thread_object(fn_class_object(), params)
Waiting for threads to finish
/ Start thread t1
    std::thread t1(callable);
 
    // Wait for t1 to finish
    t1.join();
 
    // t1 has finished do other stuff

No comments:

Post a Comment