cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

C is getting auto!

Open lefticus opened this issue 2 years ago • 1 comments

and it's weird

auto auto i = 5;
auto int j = 6;

lefticus avatar Jul 26 '22 19:07 lefticus

A related feature that C is getting is storage-class specifiers for compound literals. As you might remember, one of the big things keeping C++ from adopting compound literals is that they were always block scoped. C++ couldn't change that itself, because that would ruin C compatibility which was the biggest selling point of the feature.

// Since compound literals are block scope, this was technically illegal according to the standard,
// but Clang and GCC allow it as an extension.
static struct foo x = (struct foo){1, 'a', 'b'};

But this new feature allows you to set the storage class specifier like so:

// This is legal now! But not implemented yet. :(
static struct foo x = (constexpr struct foo){1, 'a', 'b'};
// And obviously works much better with auto.
static auto x = (constexpr struct foo){1, 'a', 'b'};

So, I'd expect to see some movement on adding this for C++26 (if it can get through the backlog).

Nobody1707 avatar Aug 15 '22 16:08 Nobody1707