Permutations of list of elements with repeated values
June 14, 2012
Uniform Initializer lists in C++11
June 24, 2012

C++11 feature – automatic type detection and decltype

C++11 is the latest version of C++ language originally designed by Stroustrup. I read an interview of him where he said that the latest standard (C++11 or C++0x) feels like a new language.
I will be publishing the new features added to the core language and to the STL in following articles in next few days. The first change (in the core language) which I want to focus today is the introduction of automatic type detection.
1. Automatic Type detection (or Type Interface)
Till the last standard (C++03) we must to specify the data type of the object we are defining.

In C++11, if the data type of the object can be derived from the initialization, then we can declare it as auto (thus defining an object without explicitly mentioning its type).

For example, in the below declaration:

    auto a = 10;

Data type of a will be int, because that is what we are initializing it with.
such automatic typing comes very handy when the data type is very verbose (long and difficult to read), like below code of C++03

    void func(const vector<pair<string,int>> &va)
    {
        vector<pair<string, int>>::const_iterator ca=va.begin();
    }
can be very neatly written in C++11 as:
    void func(const vector<pair<string,int>> &va)
    {
        auto ca=va.begin();    // using auto
    }

The type of iterator was well defined by the value it is being initialized with.
Another addition to the language is the keyword decltype, which can be used to determine the type of expression at compile time.

For example, Consider the below code
    int a = 10;
    decltype(a) b = 20;
What decltype is telling to the compiler is that get the data type of a and use it to define variable b.
So the function func above can also be re-written using decltype as below:
    void func(const vector<pair<string,int>> &va)
    {
        decltype(va) ca=va.begin();
    }

I will be covering more new features of C++ in later articles. Let me know your feedbacks / comments / questions. 🙂

Note: The auto is different from the auto keyword which was available in earlier versions (and in C language) that talks about the scope of an object to be automatic and is available in C & C++ since Pre-ANSI era. C++11 has actually removed the old meaning of auto to avoid confusion. auto now declares an object whose type is deducible from the initializer.
 

Next: Uniform initializer list in C++11

4 Comments

  1. […] comments Jun 242012   In my previous article about C++11 standard I talked about automatic type detection and keyword decltype Today I will take another major enhancement in C++11 called the Initializer […]

  2. […] comments Jun 252012   In my previous articles about C++11 standard I talked about automatic type detection , decltype keyword and uniform initializer list introduced in C++11. Lets see the Range-based for […]

  3. […]  Add comments Jun 262012   In my earlier articles on C++, I have written about the automatic type detection, uniform initialization list and Range based for loopin […]

  4. […] Lambda functions and expressions in C++11  Articles, C++  Add comments Jun 272012   In my opinion, the single biggest addition to C++11 is the Lambda function and expressions (for more features in C++ click here…). […]

Leave a Reply

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