C++ Logo

std-proposals

Advanced search

[std-proposals] Named auto

From: Oleksandr Koval <oleksandr.koval.dev_at_[hidden]>
Date: Tue, 27 Sep 2022 15:46:17 +0300
Hi, C++20 introduced angle brackets for lambdas to simplify access to
argument
types. How about going further and allowing to bind type name to `auto`
placeholder in various contexts? Motivation for it is the same as for
having it
for lambdas.
For example:

```cpp
// simple variables
auto<T> v = GetValue();
// same as
auto v = GetValue();
using T = decltype(v);

const auto<T>& v = GetRef();
// same as
const auto& v = GetRef();
using T = std::remove_cvref_t<decltype(v)>;

// parameters
void f(auto<T> v);
// same as
void f(auto v){
    using T = decltype(v);
}
// or
template<typename T>
void f(T v);

// structure bindings
auto<T1, T2> [key, value] = GetPair();
```

Syntax `auto<T>` looks weird at first glance but you can think about it as
of
`identity<T>`. Of course this is just a sketch, maybe there's a better way
to
write it.
We can also have `decltype(auto<T>)` and support parameter packs as well.

-- 
Regards,
Oleksandr Koval.

Received on 2022-09-27 12:46:30