heretoinfinity
heretoinfinity

Reputation: 1746

When not to use auto as described in Bjarne Stroustrup book: A Tour of C++

I was reading A Tour of C++ and got confused as to the reason to use auto.

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

I get the part that if we are in a large scope, we want people to see the types but I don't get the part about being explicit about a variable's range? What's meant by range here? Is it the same as precision?

EDIT: this question was closed but what the question I have asked is directed at what the author means and the example provided. Nothing broad about it. I don't see how that can stray into opinion-land as I am not asking why auto should ever be used but what is meant by range. The comments provided provide an answer.

Upvotes: 2

Views: 223

Answers (1)

David Grayson
David Grayson

Reputation: 87406

The "range" of a variable in that sentence refers to the minimum and maximum values it can hold. For example, the range of a signed char is typically -128 to 127.

Upvotes: 2

Related Questions