C auto keyword Why is it magic

0 votes
Auto has always been a strange storage duration specifier that served no purpose in all of the materials I used to learn C++.

However, I recently came across code that used it as a type name by itself.

I tried it out of curiosity, and it takes on the type of whatever I assign to it!

STL iterators and, well, anything that uses templates is suddenly ten times easier to write.

It's as if I'm programming in a 'fun' language like Python.

Where have I been looking for this keyword my entire life?

Will you break my heart by telling me it's only available in Visual Studio or that it's not portable?
Jun 6, 2022 in C++ by Nicholas
• 7,760 points
679 views

1 answer to this question.

0 votes

Auto was a keyword that C++ "inherited" from C and had been around for a long time but was almost never used because there were only two possibilities: it wasn't allowed or it was assumed by default.

C++11 introduced the usage of auto to denote an inferred type.

Similarly to how template type deduction works for function templates, auto x = initializer deduces the type of x from the type of initializer. 

Consider the following function template:

template<class T>
int whatever(T t) { 
    // point A
};

T has been allocated a type at point A based on the value supplied for whatever's argument. 

When you execute auto x = initializer;, the type for x is deduced from the type of initializer that was used to initialize it.

This implies that for every compiler that attempted to implement C++98/03, much of the type deduction mechanisms needed to construct auto were already present and utilised for templates. 

As a result, adding support for auto appears to have been rather simple for virtually all compiler teams—it was implemented rapidly, and there appear to have been few errors associated with it.

answered Jun 21, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Is C++ considered weakly typed? Why?

C++ has always struck me as one ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
1,665 views
0 votes
0 answers

Why is the size of an empty class in C++ not zero?

Why does the following output 1? #include <iostream> class Test { }; int ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
559 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
842 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
889 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6, 2022 in Java by narikkadan
• 63,600 points
3,143 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
2,932 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
481 views
0 votes
1 answer

The new syntax "= default" in C++11

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list. I'll give you an example to demonstrate the difference: #include <iostream> using namespace std; class A { public: ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
488 views
0 votes
1 answer

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
688 views
0 votes
1 answer

Why is Turbo C++ showing this error in DOSBox on my Mac?

Your installation must be defective!  I have a Mac, and I'm typing this on it while using TurboC++.  Consider uninstalling and then reinstalling the programme. Download the package in the same way as you would a.dmg programme from the internet.  (For example, drag and drop the programme into the Applications folder)  Ascertain that your Applications folder is global to your system.  This is what I mean: When in Finder, select the "GO" option from the top menu bar. From the drop down option, choose "Computer." In the newly opened window, click on your hard disc. There is a "Applications" folder there.  That's where you should put TurboC++. Go to Launchpad, and start Turbo C++. ...READ MORE

answered Jun 28, 2022 in C++ by Damon
• 4,960 points
1,144 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP