Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Stroustrup reason for using auto when defining a variable

I am reading Bjarne Stroustrup "The C++ programming language" book, and it is mentioned that one of the reasons for using auto in a variable definition is:

The definition is in a large scope where we want to make the type clearly visible to readers of our code.

What is the meaning of large scope here? and anyone has an example for this statement, as I feel it is not clear how using auto is making the type clearly visible to readers of the code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You took only part of the quote from the book. The entire quote is:

We use auto where we don’t have a specific reason to mention the type
explicitly. ‘‘Specific reasons’’ include:

  • The definition is in a large scope where we want to make the type clearly visible to readers of our code.
  • We want to be explicit about a variable’s range or precision (e.g., double rather than float).

The part you mentioned in the question is the Specific reason when auto should not be used, so exactly the opposite you thought.

I.e., when you want to make the type clearly visible to readers, you should not use auto.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading