hatcat / cg30_issues

The public issues repository for the book "Beautiful C++"
4 stars 1 forks source link

That usage of auto is not... beautiful #47

Open j-silver opened 2 years ago

j-silver commented 2 years ago

Beauty is in the eye of the beholder.

I almost always use auto (and I have a hard time with my line manager who opposes it with nonsensical reasons): we often don't need to spell out the type of local variables returned by functions, especially when they would be insanely verbose, plus, as you briefly explain at page 248, automatic type deduction avoids the implicit conversions we would have if we initialised with the wrong type.

However, the usage of auto at page 62 and later seem all but beautiful to me :-)

auto options = std::ifstream(filename);
...
auto key = std::string{};
auto value = std::string{};

Really? It's longer than

std::ifstream options{filename};
std::string key;

and we're not gaining anything, apart from a vague consistency in style, or are we trying to mimic Rust?