Automatic Type Detection in C++ 11

  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
b) nullptr
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.

For example,

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.

For example,

#include <iostream>

using namespace std;

class mynewclass
{
    int x;
    char y;
    double z;
public:
    mynewclass(int a, char b, double c);
    void disp();
};

mynewclass::mynewclass(int a, char b, double c)
{
    x = a;
    y = b;
    z = c;
}

void mynewclass::disp()
{
    cout << x << "n";
    cout << y << "n";
    cout << z << "n";
}

int main()
{
    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);

    m.disp();
    n.disp();
    o.disp();
    p.disp();
    q.disp();
    r.disp();

    return 0;
}


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.
 
For example,
    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 363 times, 1 visits today)

7 comments

  • mynewclass m = mynewclass(49, 'a', 9.8);
    and
    mynewclass m(49, 'a', 9.8);

    are just two different ways to initialize an instance of mynewclass. There is nothing wrong with the first method. On the contrary, the first method is the normal form and the second method is its abbreviated form.

    In fact, you can see the following example from page no:- 227 of Bjarne Stroustrup's book "The C++ Programming Language":-

    Date today = Date (23, 6, 1983);
    Date xmas (25, 12 ,1990); // abbreviated form

    As you can see in the excerpt above, the first method utilizes the method used in this article, while the method suggested by you is at the second place and has the comment "abbreviated form".

    It means that Bjarne Stroustrup, the creator of C++, himself suggests the first method for initialization of an instance of a class.

    I suppose you've understood that.

    Concerning the "auto" keyword, the aim of this article is to provide a description of what the "auto" keyword is about. To use or to ignore it, depends upon individual programmers.

  • This is a really bad way to initialize an instance of a class:
    mynewclass m = mynewclass(49, 'a', 9.8);
    That will usually create a temporary mynewclass and then use the copy constructor to initialize m, and destroy the temporary.
    It should be:
    mynewclass m(49, 'a', 9.8);

    IMO, 'auto' should only be used where type information isn't useful to the reader.

  • Lazy_Programmer

    C++ 11? C++ 14? Why don't they create a new language altogether instead of all these modifications? Before I manage to get used to one version, another pops up. LOL

  • Thanks in support of sharing such a fastidious
    thought, piece of writing is fastidious, thats why i
    have read it fully

  • Very nice post. I just stumbled upon your weblog and wished
    to say that I have really enjoyed surfing around your blog posts.

    After all I will be subscribing to your rss feed and I
    hope you write again soon!

  • google adwords account

    If some one wants expert view concerning blogging and site-building after that
    i recommend him/her to pay a visit this website, Keep up the pleasant work.

Leave a Reply

Your email address will not be published. Required fields are marked *