Sunday, January 8, 2017

C++11 Features (Part 1)


Here i'm listing some of the C++ 11 features we can use frequently.

1.Auto
2.Range Based for loop
3.Strongly typed enums
4.Forward enum declaration

To use C++ 11 features when compiling in commad line use any of the following flags

  $g++ -o myprog myprog.cpp -std=c++0x  (or)  
  $g++ -o myprog myprog.cpp -std=gnu++0x (or)  
  $g++ -o myprog myprog.cpp -std=c++11    

Auto:
Before C++11 auto keyword is used in the storage class, from C++11 it is used as type inference, means automatic deduction of data type of an expression.
We can use auto inside the blocks, namespaces, and for loop initialization etc. Using auto we can reduce writing the some complex type declarations like iterators intialization in loop.
We cannot use auto in return type in fuction definition, but we can use auto for the return type when calling function.

Example:
 #include<iostream>  
 #include<map>  
 using namespace std;  
 int add(int a,int b)  
 {  
 return a+b;  
 }  
 auto g=7;  
 int main()  
 {  
 int arr[5] = {0,1,2,3,4};  
 std::map<int,std::string> mapNumbers;  
 mapNumbers[1] = "One";  
 mapNumbers[2] = "two";  
 mapNumbers[3] = "three";  
 mapNumbers[4] = "four";  
 auto temp = add(1,2);  
 cout<<g<<endl;  
 for(auto i : arr) //range based for loop  
 {  
 cout<<i<<" ";  
 }  
 cout<<endl;  
 for(auto itm: mapNumbers) //range based for loop  
 {  
 cout<<itm.first<<" "<<itm.second<<"\n";  
 }  
 return 0;  
 }  

$ g++ -o autoexample autoexample.cpp  -std=c++0x
$./ autoexample

7
0 1 2 3 4
1 One
2 two
3 three
4 four

Range Based for loop:
Range based for loops allows us 'foreach' like accessing of elements of an array, list, initializer list or containers. When we are doing operations on the elements of the array/containers but we don't need index of the array/containers we are accessing, then this range based for loops are useful.

Example:
 #include<iostream>  
 #include<vector>  
 using namespace std;  
 int main()  
 {  
 std::vector<int> vec = {1,2,3,4,5};  
 float farr[] = {6,7,8,9,10};  
 for(int i: vec)  
 {  
 cout<<i<<" ";  
 }  
 cout<<endl;  
 for(float f: farr)  
 {  
 cout<<f<<" ";  
 }  
 cout<<endl;  
 for(int t: {1,3,5,7,9})  
 {  
 cout<<t<<" ";  
 }  
 cout<<endl;  
 return 0;  
 }  


Strongly typed enum:
To overcome the limitations and drawbacks of the old style enums C++11 introduces strongly types enums.
In Old style
  • enums are implicitly converted into integers
  • enum constants are exposed to surrounding name scope, so name clashes happens
                enum colors { RED,GREEN, ORANGE};
                enum fruits { ORANGE, APPLE, BANANA}; // name clash for ORANGE
  • We cannot specify the underlying enum type (size of enum)
So to overcome these drawbacks, c++11 introduces strongly typed enums.
Ex: enum class colors { RED,GREEN, ORANGE };
       enum class xyz : unsigned long {x1, x2, x3};
       enum class abc : char {a1, a2, a3};
We can access this enum with scope (colors::RED) or like old style (RED)


Forward enum declaration:
We can have forward declaration of enums like below, and after that we will define   enum xyz
 enum class xyz ;  
 void fun(xyz var);  

Example:
 #include<iostream>  
 using namespace std;  
 class Sample  
 {  
 public:  
 enum class color; //forward declarion  
 void fun(color c)  
 {  
 cout<<"fun "<<(int)c; // no implicit conversion, explicitly convert  
 }  
 enum class color: int  
 {  
 RED=1, GREEN, ORANGE  
 };  
 enum class fruits : char  
 {  
 APPLE=1, MANGO, ORANGE // same name ORANGE, this leads to error in old style  
 };  
 };  
 int main()  
 {  
 Sample s;  
 s.fun(Sample::color::ORANGE);  
 }  


1 comment:

  1. As claimed by Stanford Medical, It is in fact the ONLY reason this country's women get to live 10 years longer and weigh an average of 19 kilos less than we do.

    (And by the way, it is not about genetics or some secret diet and absolutely EVERYTHING related to "how" they are eating.)

    P.S, What I said is "HOW", and not "WHAT"...

    Click on this link to see if this little quiz can help you discover your true weight loss possibility

    ReplyDelete