2

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.

4
  • 2
    I think that is supposed to be a reason not to use auto. Sentence prior: "We use auto where we don’t have a specific reason to mention the type explicitly. ‘‘Specific reasons’’ include:". "specific reasons" refers to reasons to mention the type explicit and therefore not use auto. Commented Sep 16, 2022 at 6:12
  • @OP I think you should read carefully the prior sentence. As pointed out by user17732522, the author is giving some "specific reasons" to use auto. Commented Sep 16, 2022 at 6:13
  • Voting to close as a typo as this seems to be a misunderstanding of OP and this question won't help future readers which can read the prior sentence before asking this question. You should also include whole paragraph in your question instead of just cherry picking and quoting a sentence out of context. Commented Sep 16, 2022 at 6:15
  • Thanks, I misinterpreted the sentence
    – 0x8F
    Commented Sep 16, 2022 at 6:32

1 Answer 1

9

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.