C ++ 11 initialization with auto

In C ++ 11, we recommend using auto for a variable type,
does this also apply when initializing type of type of class and vector?

I would like to write the following:

auto a = 10; auto b = MyClass(); auto c = vector<int>{1, 2, 3}; 

instead:

 auto a = 10; MyClass b; vector<int> c = {1, 2, 3}; 
+6
source share
1 answer

auto is just a convenient shortcut to simplify features like

 VeryLongClassName *object = new VeryLongClassName(); 

Now it will be

 auto *object = new VeryLongClassName(); 

No reason to write

 auto a = 10; auto b = MyClass(); auto c = vector<int>(); 

because it is longer and harder to read than

 int a = 10; MyClass b; vector<int> c; 
+13
source

Source: https://habr.com/ru/post/952914/


All Articles