C++ Logo

std-discussion

Advanced search

Re: Rationale for no auto arrays?

From: Peter Sommerlad (C++) <"Peter>
Date: Wed, 23 Feb 2022 10:03:45 +0100
use std::array

Eyal Rozenberg via Std-Discussion wrote on 23.02.22 01:13:
> tl;dr: Why can't we write:
>
> auto arr[2] = { 1, 2 };
>
> and have that compile?
>
> --------
>
> Longer version:
>
> When the auto keyword was repurposed for C++11, and perhaps in later
> discussions about expanding its use - was the possibility of using auto
> for the types of arrays discussed? If so, can I get a reference to such
> discussion, or to papers accepted/rejected which discuss this?
>
> I don't quite see why we these statement:
>
> auto my_initializer_list = { 1, 2 };
> int arr[2] = { 1, 2 };
>
> should compile (the former in C++17), but
>
> auto arr[2] = { 1, 2 };
>
> should not. And this seems doubly strange, seeing how we can use all of:
>
> const auto
> auto&
> auto*
>
> in variable definitions. Why is pointerification ok, but arrayfication
> isn't?
>
> Eyal
>
> PS - My motivation is wanting to initialize a fixed-size array with
> expressions which return a complex type I would rather not have to
> specify explicitly.
>


-- 
Peter Sommerlad
Better Software: Consulting, Training, Reviews
Modern, Safe & Agile C++
peter.cpp_at_[hidden]
+41 79 432 23 32

Received on 2022-02-23 09:03:49