An Overture to C Programmers
xliii
HolmesIV* make_mike(int sense_of_humor) {
--
snip
--
}
int main() {
auto mike = make_mike(1000);
free(mike);
}
Listing 10: A toy program initializing a variable with the return value of a function
The
auto
keyword is easier to read and is
more amenable to code refac-
toring than explicitly declaring a variable’s type. If you use
auto
freely while
declaring
a function, there will be less work to do later if you need to change
the return type of
make_mike
.
The case for
auto
strengthens with more complex
types, such as those involved with the template-laden code of the stdlib. The
auto
keyword makes the compiler do all the work of type deduction for you.
N O T E
You can also add
const
,
volatile
,
&
, and
*
qualifiers to
auto
.
Dostları ilə paylaş: