Nov 25, 2025

[C++23] explicit decay-copy

auto(...)
creates a temporary copy (a prvalue) of the variable inside it.

By wrapping it in auto(...), the compiler forces a copy of that variable.

Making a copy drops references (&).

Making a copy drops top-level const.

Therefore, decltype(auto(...)) yields the clean, raw type (e.g., std::integral_constant<T, v>) without const or &.



#include <type_traits>

int main() {
    // A const variable
    const int x = 10;

    // 1. Normal decltype
    // Retains 'const'
    using T1 = decltype(x); 
    static_assert(std::is_same_v<T1, const int>); 

    // 2. C++23 auto(x) cast
    // Forces a copy, shedding the 'const'
    using T2 = decltype(auto(x)); 
    static_assert(std::is_same_v<T2, int>); 
} 

No comments:

Post a Comment

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