About "auto"

Is there any reason to use the storage class
auto??


The use of auto to deduce the type of a variable from its initializer is obviously most useful when that type is either hard to know exactly or hard to write.
...
When the type of a variable depends critically on template argument it can be really hard to write code without auto. For example:
1
2
3
4
5
6
	template<class T, class U> void multiply(const vector<T>& vt, const vector<U>& vu)
	{
		// ...
		auto tmp = vt[i]*vu[i];
		// ...
	}

The type of tmp should be what you get from multiplying a T by a U, but exactly what that is can be hard for the human reader to figure out, but of course the compiler knows once it has figured out what particular T and U it is dealing with.

_ Stroustrup http://www.stroustrup.com/C++11FAQ.html#auto


... it’s more than just a convenience when a type has an unknown or unutterable name, such as the type of most lambda functions, that you couldn’t otherwise spell easily or at all.

1
2
3
4
5
 // C++98
binder2nd< greater > x = bind2nd( greater(), 42 );

// C++11
auto x = [](int i) { return i > 42; };


Sutter http://herbsutter.com/elements-of-modern-c-style/
Thanks man.. Thanks
Topic archived. No new replies allowed.