Tuesday, 26 March 2013

C++ 11 ( some important features ) Part1

C++ 11 is the newest version of the standard C++ programming language.

On 12 August 2011, International Standard Organization (ISO) approved this new version of C++.

C++11 has added so many new features to the standard C++ language. Here we are not going to cover all the features but I will share some important features that have been asked to me when I have attended the C++ interview.

Compiler that support C++11 are Visual Studio 2010 Express, g++ 4.7 ( and higher versions ).

Let’s start with auto keyword:-

1) auto:-
            In standard C++, auto is a storage class specifier. auto tells the compiler about the life time and visibility of the object.

                          int main()
                              auto int x = 0; // Explicitly declared as auto 
                              int y = 0;         // Implicitly auto
                              return 0;

           C++ 11 has added a new meaning of auto keyword.
           a) auto variable_initializer :-

                   auto tells the compiler about to automatic detect the type of its variable from its initializer.
                   example :-
                                   auto  i = 5 ;       // i will be of type int
                                   auto  j = 5.5 ;    // j will be of type double
                                   auto  k = j ;      // k will be of type double
                                   auto  a = "hello" ;     // a will be of type const char *

           b) auto function_name( parameters ) -> return_type :-

                   auto also works as a part of the trailing return type in functions.

                           The trailing return type feature is useful in a scenario where the return type of a function template can not be generalized if the return type depends on the types of the function arguments.

using namespace std;

template< class T, class U >

auto add(T t, U u) -> decltype(t + u) // -> decltype(t+u) we are trailing the return type
    return t + u;

int main()
auto i=5;
auto b=fun1(i,i);
return 0;	

                           In the above code, the return type of add() function is auto. We are trailing the return type with the help of decltype type specifier. 

2) decltype (expression) :-

            The decltype is a type specifier, which yields the type of a specified expression.

            Compiler uses some rules to determine the type of the expression, those rules are as follows:-

                1) If the expression parameter is an identifier, decltype(expression) is the type of the entity named by expression.

                2) If the expression parameter is a call to a function or an overload operator function, decltype(expression) is the type of the function.

                3) If the expression parameter is a rvalue, decltype(expression) is the type of the expression.

                    If the expression parameter is a lvalue, decltype(expression) is a lvalue reference to the type of the expression.

                                  char value;
                                  const char && func();
                                  struct T { int i; }
                                  const T * t = new T();

                                 decltype( func() ); // Type: const char && (type of function func() )
                                 decltype( value ); // Type: char (type of variable value )
                                 decltype( t->i );   // Type:  int (type of member access )
                                 decltype( ( t->i ) ); // Type: const int & ( The inner parentheses cause the statement to be evaluated as an expression instead of a member access and because t is declare as a const pointer, the type is a reference to a const int  )

decltype is useful when declaring types that are difficult or impossible to declare using standard notation, like lambda-related types or types that depend on template parameters.

Stay tuned, to know about some more important C++11 features.

Please share the blog with your buddies, so that they can also learn some new things.