The C++ 11 standard introduced several new features in C++. The introduction of these features changed C++ significantly. Several common issues were solved, and C++ programming became much easier.
Some of these new features are :
a) Automatic Type Detection
c) Lambda Expressions
d) Uniform Initialization, etc.
Here, we shall discuss “Automatic Type Detection”.
The auto keyword
We all know that prior to C++ 11, auto keyword was used as a storage class specifier. However, it was rarely used because a variable whose storage class was not explicitly specified was automatically accepted as an auto variable (and hence, there was no need to use this keyword).
In C++ 11, the auto keyword was assigned a new meaning. It is no longer used as a storage class specifier and is now used to declare variables without specifying their data type. It can also be used with user-defined data types.
Earlier, we used to write :
int x = 79;
double y = 24.5;
char z = 'c';
Now we can simply write :
auto x = 79;
auto y = 24.5;
auto z = 'c';
As you can clearly see from the examples above, you don’t need to explicitly declare a variable as int, double, char …, the auto keyword will be sufficient. it will automatically deduce the data type of a variable depending on the value assigned to it during initialization.
Therefore, if you assign an integer to a variable, it data type will be automatially detected and set to int. If you assign a character, it will be set to char.
Now, this feature might not appear so important as long as you consider only the basic data types like int, double etc. However, when you think about its role in case of user-defined functions, you’ll be glad it exists now.
using namespace std;
mynewclass(int a, char b, double c);
mynewclass::mynewclass(int a, char b, double c)
x = a;
y = b;
z = c;
cout << x << "n";
cout << y << "n";
cout << z << "n";
mynewclass m = mynewclass(49, 'a', 9.8);
mynewclass n = mynewclass(68, 'd', 1.7);
mynewclass o = mynewclass(57, 'b', 2.3);
mynewclass p = mynewclass(34, 'e', 4.7);
mynewclass q = mynewclass(12, 'h', 9.3);
mynewclass r = mynewclass(95, 'i', 6.2);
As you can see in the above case, it is annoying to have to type “mynewclass” so many times.
Here, the auto keyword provides a huge relief.
You can use
auto m = mynewclass(49, 'a', 9.8);
auto n = mynewclass(68, 'd', 1.7);
auto o = mynewclass(57, 'b', 2.3);
auto p = mynewclass(34, 'e', 4.7);
auto q = mynewclass(12, 'h', 9.3);
auto r = mynewclass(95, 'i', 6.2);
Isn’t that much better? And if you had to declare and initialize objects of several different classes, say myfirstclass, anotherclass, lastclass etc., wouldn’t it be much easier to write auto in place of all these class names? Also, the significance of this keyword becomes more apparent if you’re using templates.
The decltype operator
The C++ 11 standard introduced an operator named decltype. This operator takes an expression and ‘captures’ its type.
decltype (m) s = mynewclass(13, ‘f’, 34.2);
Here, decltype ‘captures’ the data type of m and uses it to declare a new variable s and initialize it.
We can also use decltype for a more useful purpose, as shown below.
typedef decltype (m) wow;
wow s = mynewclass(13, 'f', 34.2);
Here, we extract the data type of m and assign it to the new type wow, which can then be used to declare variables (which are of the same data type as the variable m).
So, now it is clear that the new meaning assigned to the keyword auto has made it much more useful. Also, the new operator named decltype is a very useful addition to C++.
The next article discusses Lambda Expressions in C++ 11.
(Visited 369 times, 1 visits today)