C++23 introduced `auto(x)` as a decay-copy cast expression via P0849, but `auto{x}` behaves differently due to brace-initialization semantics. While `auto(x)` makes a copy of `x` (equivalent to `T(x)`), `auto{x}` constructs a `T` from an initializer list `{x}`, which for types like `std::vector<std::any>` means wrapping `x` as an element rather than copying it. MSVC currently gets this wrong by calling the copy constructor in both cases. The post also covers edge cases: `return auto(x)` does not implicitly move unlike `return (x)`, multi-argument `auto(...)` is invalid, and `auto a = {1,2,3}` remains a historical inconsistency in the language. The practical takeaway is to use parentheses for copies in generic code and curly braces only when initializing from a sequence of elements.

4m read timeFrom quuxplusone.github.io
Post cover image

Sort: