I am/was a AAA skeptic as well, and have been waiting for Concept x = ... (or now Concept auto x = ...) as the sweet-spot solution.Now that we have:auto x = expr;Concept auto x = expr;doesn'tint x = expr;fit the "pattern" better thanauto x = int{expr};I think not. If I see this code:int x = expr;I’m not certain which of these is the author’s intent:
- No matter what expr evaluates to, I want x to be an int.
- This is legacy code from before auto existed.
- I’m guessing that expr returns an int ...
- ... and I’m not using auto out of programming style momentum.
But if I see this:auto x{int{expr}};I’m pretty certain that the meaning is, "No matter what expr evaluates to, I want x to be an int."
What is the value ofauto x = int{expr};This better conveys the intention that you really want x to be int without regard to the type of expr. The alternative “int x = expr;” just invites someone to notice that expr (either now or the future) might not be an int, and thus reflector it as “auto x{expr};
Two real-life examples, from Howard Hinnant, paraphrased:auto ns = nanoseconds{duration}; // duration was a chrono type, but became int, is this still what you wanted???vsnanoseconds ns = duration; // if duration is strong chrono type, does the conversion you wanted/expected, if duration is int, fails to compileAndauto sp = shared_ptr<Foo>(ptr); // ptr is a weak_ptr, need sharedvsshared_ptr<Foo> sp = ptr;When, under maintenance, ptr changes from weak_ptr to raw T* ptr, one of those lines is probably a bug, the other doesn't compile.(And yes, maybe they should have called ptr.lock() on the weak ptr, but some people think that was a poorly named function (confusing with mutex etc) and prefer the conversion style.)So I still prefer int x = expr; over auto x = int{expr};, and I think int x = expr nicely follows a pattern with auto x = expr and Concept auto x = expr.