4 ms·
My personal rules for using auto. Only use it iff: 1. the actual type is clearly visible on the right hand side. auto f = make_widget(); // it's a widget
by ensiferum 10y ago
My personal rules for using auto. Only use it iff:
1. the actual type is clearly visible on the right hand side.
auto f = make_widget(); // it's a widget
auto i = 123; // it's an int
auto x = vec3(1.0, 0.0, 0.0); // it's a vector
2. the actual type doesn't really matter so much or is complicated to type out.
auto it = vec.begin(); // it's an iterator
auto it = // some template expression
3. the actual type is not known i.e. lambdas
- Aleman360 10y agoIn a word where most new code is JavaScript, just use auto everywhere. It will be OK.
- fish2000 10y agoThese are good rules – as in liquor advertisements, it’s advisable to always accompany a declaration that uses `auto` with a rejoinder in the comments to Please Enjoy Responsibly
- slavik81 10y agoI'm not a huge fan of the first rule. It's valid as stated, but the examples are questionable. I've seen all those in real code, and they're kind of annoying. auto f = make_widget(); // is f of type widget or widget*? auto i = 123; // ok, I guess, but... // int i = 123; is shorter and more clear. auto x = vec3(1.0, 0.0, 0.0); // this can be shorter and simpler. // vec3 x(1.0, 0.0, 0.0);