Signup/Sign In

C++ 11 New Features and Concepts

Posted in Programming   LAST UPDATED: SEPTEMBER 2, 2021

    Recent years have seen C++ undergoing a change, and major credit for that goes to Microsoft. Microsoft has constantly evolved itself working on improving programming languages to match the pace of the fast-changing IT industry. C++ language is used for Kernel development, for building Operating systems features and these days for implementing Machine learning algorithms too.

    Facebook Open Source Library released Folly a few years back, which is a large collection of reusable C++ library components that are used extensively at Facebook.

    With so many new features being introduced, still developers around the world prefer using age-old features of C++ language and hence this is an attempt to introduce you to some new C++ 11 features.

    Here are the top C++11 features:

    • auto

    • noexcept

    • lambda

    • nullptr

    • override identifier

    • unordered containers

    • move

    • variadic Template

    Let's cover these briefly. We will be covering each one of them in detail in separate tutorials too.

    1. C++ 11: auto

    C++ 11 presents type inference capability with the help of the auto keyword. It means that the compiler refers by itself the type of a variable at the point of declaration. Using the auto keyword permits us to spend less time having to write out things that the compiler already knows like the data types.

    2. C++ 11: noexcept

    If a function is unable to throw an exception by any means then that function can be declared as noexcept in C++ 11 which helps to resolve unknown types of errors.

    3. C++ 11: lambda

    Anonymous function can be created in C++ 11, and it's known as Lambda function.

    So basically lambda expression allows us to define functions locally. Also, it allows to define function at the place of the call, hence eliminating much of the complexity and security risks.

    For example:

    template<class InputString, class OutputString>
    
    bool unhexlify(const InputString& input, OutputString& output)
    {
        if (input. size() % 2 !=0)
        {
            return false;
            output.resize (input. size () / 2);
            int j = 0;
            auto unhex = [] (char c) - > int {
            return c >= ‘0’ && c<= ‘9’ ? c - ‘0” :
                   c >= ‘A’ && c<= ‘F’ ? c - ‘A’ + 10 :
                   c >= ‘a’ && c<= ‘f’ ? c - ‘a’ + 10 :
                   -1;
    };

    4. C++ 11: nullptr

    One of the new features in C++ 11 is that it allows programmers to use nullptr in place of NULL or 0 for specifying a pointer which refers to no value. This is not the same as not defining any value. In earlier versions of C++, the constant 0 had the double role of constant integer and null pointer constant. In the Folly source code, all null pointers are represented by the new keyword nullptr, there’s no place where the constant 0 is used.

    For example:

    void g(int);
    
    void g(void*);
    
    g(0); // calls g(int)
    
    g(NULL); // calls g(int) as and when NULL is 0 and ambiguous otherwise
    
    g(nullptr); // calls g(void*)

    5. Override Identifier:

    With projects grow larger and larger, more number of files are incorporated to complete one task. Now while using OOP concepts with such implementation developers may by mistake override a Parent class function by putting extra argument or maybe given argument with different data types which will change the whole meaning of the function. Let's see one example to understand it clearly :

    class Parent {
    
    public:
        // user wants to override this in
        // the derived class
        virtual void udf(float a)
        {
            cout << "I am in Parent" << endl;
        }
    };
    
    class child : public Parent {
    
    public:
        // did a silly mistake by putting different datatype
        // an argument "int a"
        void udf(int a)
        {
            cout << "I am in child class" << endl;
        }
    };

    The above code will not give any Error when called as it will create new function instead.

    But the code below will give Error as override keyword is used after overriding the function

    class Parent {
    
    public:
        // user wants to override this in
        // the derived class
        virtual void udf(float a)
        {
            cout << "I am in Parent" << endl;
        }
    };
    
    class child : public Parent {
    
    public:
        // did a silly mistake by putting different datatype
        // an argument "int a"
        void udf(int a) override
        {
            cout << "I am in child class" << endl;
        }
    };

    6. Unordered Containers:

    Previously as there was no mechanism for containers to work in synchronization developers used to set manual locks to every container to avoid race condition and ambiguity now with new features of 4 types of Unordered Containers this can be handled automatically as they behave like a hash table.

    1. Unordered_map

    2. Unordered_set

    3. Unordered_multimap

    4. Unordered_multiset

    7. C++ 11: move

    The move operation is basically an optimized version of the copy. The only difference is it destroys the data after moving it to a new host. It can be more useful when implicitly generated. Avoid using user-specified move, copy or destructor classes as it may create havoc. C++ has optimized the concept of rvalue and lvalue through this move operation.

    The lvalue has some name associated with it like a variable name, while rvalue is temporary objects so no names are given there. For Vec, the implicit (generated) copy maintains the invariant whereas the implicit (generated) move does not.

    For example, if we have the code below,

    Vec x = { {1,2,3,4},2 };
    
    Vec y = x;
    
    Vec z = move(x);
    

    Here, if we have used copy method, it will preserve the invariant (y is {{1,2,3,4} 2} just like x). However, the move method does not maintain the invariant as it moves from x: the value of x has become {{},2} and 2 isn't a valid index into {}.

    8. Variadic Template

    It is one of the most exciting features as it allows a variable number of arguments to be taken for a function. It allocates memory runtime so it can take as many arguments as many you want to pass on. Let's see one example to understand it properly:

    template <typename T, typename... Types>
    
    void print(T var1, Types... var2) // Syntax for using Variadic Template
    {
        cout << var1 << endl ;
        print(var2...) ; // … shows allow all the arguments that follow
    }
    
    int main()
    {
        print(1, 2,3,4,5,6,7,8,9); // list of values to be given to arguments
        return 0;
    }

    Conclusion:

    If the changes in C++11 seem overwhelming, don't be alarmed. Take the time to digest these changes gradually. We will be covering each one of them in detail in separate tutorials.

    You may also like:

    About the author:
    Navjyotsinh Jadeja is Faculty in an Engineering College, driving cross-disciplinary research and innovation in technology, sciences. He is the Pedagogical Innovation Award Winner from Gujarat Technological University Innovation Council.
    Tags:Cpp
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS