+1 vote
in C Plus Plus by

Auto Feature of C++ 11

1 Answer

0 votes

you must specify the type of an object when you declare it. Yet in many cases, an object’s declaration includes an initializer. C++11 takes advantage of this, letting you declare objects without specifying their types:

auto x=0; //x has type int because 0 is int

auto c='a'; //char

auto d=0.5; //double

auto national_debt=14400000000000LL;//long long

Automatic type deduction is chiefly useful when the type of the object is verbose or when it's automatically generated (in templates). Consider:

void func(const vector<int> &vi)


vector<int>::const_iterator ci=vi.begin();


Instead, you can declare the iterator like this:

auto ci=vi.begin();

The keyword auto isn't new; it actually dates back the pre-ANSI C era. However, C++11 has changed its meaning; auto no longer designates an object with automatic storage type. Rather, it declares an object whose type is deducible from its initializer. The old meaning of auto was removed from C++11 to avoid confusion.

C++11 offers a similar mechanism for capturing the type of an object or an expression. The new operator decltype takes an expression and "returns" its type:

const vector<int> vi;

typedef decltype (vi.begin()) CIT;

Related questions

0 votes
asked Jan 6, 2021 in C Plus Plus by GeorgeBell
0 votes
asked Jan 24, 2021 in JAVA by rajeshsharma