Jul 11, 2022

[C++23] auto(x)

Reference:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0849r8.html
https://en.cppreference.com/w/cpp/language/auto


The auto specifier may also appear in the simple type specifier of an explicit type conversion:
auto(expr) and auto{expr}.
Its type is deduced from the expression.


Instead of writing:
void pop_front_alike(Container auto& x) {
    using T = std::decay_t<decltype(x.front())>;
    std::erase(x.begin(), x.end(), T(x.front()));
}

simply:
void pop_front_alike(Container auto& x) {
    std::erase(x.begin(), x.end(), auto{x.front()});
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.