New and Exciting features in C++ 11 / C++ 14

Discussion in 'C++' started by BiplabKamal, Jan 17, 2016.

  1. Many experienced programmers have ignored the fact that the major release C++11 has added lot of features which are part of modern programming technique. If you are passionate programmer and loves C++ as the programming language you must know those new features. The objective of this topic is to help programmers who are yet to explore C++11 and C++14 features by simplifying the learning curve.

    Though Bjarne Stroustrup started creation of the C++ compiler in the year 1979, the first international standard for C++, was published by the C++ standards committee in 1998 and is known as C++98. After that, there was no major release of C++ until 2011 when the C++11 standard was released. C++11 added lot of new features which are the main focus of this topic. C++14 was released in 2014 as an extension to C++11, mainly with bug fixes and small extension.

    After C++14, a major revision, known as C++17, is planned for 2017.

    Here is the revision history of C++ :
    • C++98 1998
    • C++03 2003
    • C++07 2007
    • C++11 2011
    • C++14 2014
    • C++17 2017 (Planned)
    Following list includes major enhancement features of C++ introduced in C++11 and C++14. The reader should be already familiar with C++ programming.

    1. auto ( C++11: Variable type – uses type deduction )



    auto is nothing but an extension of type deduction already available prior to C++11 while resolving template arguments in a function template instantiation without specifying the template arguments explicitly. auto works for variable initialization where you don't specify the type of the variable but it is deduced from the type of initialization expression. The type of the variable is deduced by the compiler. For example:
    Code:
        vector<int> V;
        auto itr = V.begin();// the type of 'itr' is  vector<int>::iterator
    

    2. decltype (C++11: Keyword to determine the type of an expression)



    In Generic Programming using template it is difficult to specify types which are dependent on template parameters. Decltype keyword is enhanced version of typeof operator,
    decltype(expr) var; // Here the type of var will be exact type of the expr. It helps programmer from typing long, complex type name which are obvious.

    3. Function return type deduction (C++14 : auto as return type)



    Return type of a function can be declared as auto where the return type will be deduced from the return statement

    4. nullptr (C++11: Keyword – alternative to null/NULL)



    Initializing a pointer to point nothing was done with NULL macro which is most of the time defined as 0 or 0L in some library. NULL is not part of C++ language, so you cannot use it without including that libray. C++ 11 introduces a strong typed keyword 'nullptr' which cannot be converted to integer but bool. Also NULL does not work as intended when type deduction from pointer type.

    5. Lamda expression ( C++11, C++14: Unnamded function)



    Lamda functions (or lamda expression) are unnamed functions which can be used where a function object or Functor is expected. They are handy to pass as arguments to functions which takes function object as input.

    6. alias (C++11: alternative to typedef)



    alias creates a new name for already existing types similar to typedef. Following 2 lines are similar where both are creating a new identifier alias for the same type (function pointer)
    Code:
    using func = int(*)(int);// C++11
    typedef int(*func)(int);
    func fptr = [](int i) {return i*i;};
    
    In addition alias can be templated, but typedef cannot be. Following declaration and assignment uses templated alias.
    Code:
    template<typename T>
    using func = T (*)(T);
    func<int> fptr = [](int i) {return i*i;};
    

    7. override and final (C+11: Helps programmers to avoid mistakes while overriding)



    Prior to C++11 you could have declare a function in a derived class with same name but different parameters of a virtual function in base class with the intention of overriding the base function. Compiler will not generate any error/warning and treat it as a new function. Override identifier helps you to specify your intention so that compiler validate it. Final identifier restrict further overriding of of virtual method or inheriting the class it self.

    8. constexpr (C++11: Generalized constant expression)



    Now you can declare a function or a constructor as constexpr which ensures that the function is a compile time constant which means the function return value cannot be void, it must have a single return type and the function body cannot declare variables or define types. Following code is valid with C++11-
    Code:
    constexpr int NoOfElements() // Returns number of elements
    {
    	return 100;
    }
    
    int BufferArry[NoOfElements() * 5];// An array with 5 times number of elements
    

    9. smart pointer (C++11: unique_ptr, shared_ptr, weak_ptr)



    Smart pointers are objects which manage underlying pointers including deletion, still can be used like a raw pointer. It helps in avoiding memory leaks. There are 3 types of smart pointers – unique_ptr, shared_ptr and weak_ptr.

    10. rvalue reference (C++11)



    lvalue is an object which has a name and memory address but rvalue is temporary object without a name and normally canot get the memory adress of the object. We could not delare a referance to rvalue priorot to C++11 and could not modify rvalue. C++11 introduces rvalue reference which enables us to modify the rvalue object.

    11. move semantics (C++11)



    move semantics use rvalue reference and introduce move constructor and move assignment operator which are alternative and efficient versions of copy constructor and copy assignment operator. They are helpful when a deep copy is required to create a lvalue object from a rvalue object.

    12. Range based for loop (C++11: simpler for loops for iterator based containers)



    This is improved version of for loop and similar to foreach iteration and is used to iterate through the elements of an array or collection based container where iterator, begin(), end() are implemented. This is very useful when you just want to go through element of an array or collection without bothering the indexes, iterator and number of elements. Using auto you can even avoid bothering the exact type of elements.

    13. Strongly typed and scoped enum (C++11: enum vs enum class)



    Normal enums are like constant integers and are exported to it's scope. So you cannot declare enums with same name within the scope because of name collision. Strongly typed enum enables you to define the scope of enumerator values within a class level.

    14. Concurrency (C++11)



    C++11 has added the thread library as part of the standard library for multi threading programming. Now programmer may avoid platform API to create threads and synchronizing thread execution. This makes easier to write code for multiple platforms. std::thread is the core of the thread library

    The features described here are not exclusive. There are other features included in C++11 & C++14
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice