Even more auto
It seems the usage of the auto keyword of C++11 occasionally is still
the subject of discussions.
As being someone who likes using “almost always auto” (AAA), I’m really
glad I do not have people who tell me when I’m allowed to use auto and when not.
After having recently seen yet another discussion about the subject, I later happened to stumble upon an older CppCon 2014 talk by Herb Sutter, where Herb also talked about the usage of the auto keyword.
The title of the talk was: “Back to the Basics! Essentials of Modern C++ Style”.
I think Herb pretty much nailed it in this talk.
I also do like his “left-to-right auto style” a lot (slide on page 16). We’ve applied it in the source code of our UML Editor!
Example 1
Compare this original snippet of C++ statements from our ScreenCanvas
module:
DCfromWindow dc{ itsWindow };
GdiObjectOwner<HBITMAP> bm{ ::CreateCompatibleBitmap(dc, size.cx, size.cy) };
DcOwner mdc{ ::CreateCompatibleDC(dc) };
GdiObjectSelector<HBITMAP> bms{ mdc };
bms.Select(bm.get());
which I changed to
auto dc = DCfromWindow{ itsWindow };
auto bm = GdiObjectOwner<HBITMAP>{ ::CreateCompatibleBitmap(dc, size.cx, size.cy) };
auto mdc = DcOwner{ ::CreateCompatibleDC(dc) };
auto bms = GdiObjectSelector<HBITMAP>{ mdc };
bms.Select(bm.get());
Example 2
We previously had somewhere:
d1::fPoint pos = itsOwner->GetPositionOf(*this);
I changed that to the (semantically 100% equivalent!):
auto pos = d1::fPoint{ itsOwner->GetPositionOf(*this) };
Hints:
GetPositionOf()returns ad1::Pointd1::fPointhas a converting constructor, which takes ad1::Point
Notice how the auto style version makes the conversion very explicitly readable.
Isn’t that nice? In any case: I like it a lot!
I definitely recommend watching Herb’s talk. Even though it is from 2014, I think it still applies today. Congrats to Herb for that talk!
See also Herb’s blog posting (section 4), about the topic.
(last edited 2025-09-27)