tl;dr: What is the hindrance to declaring an auto
array?
I'm a very sparse auto
user (basically with the exception of iterators where it's really helpful only using it when left absolutely no other choice, such as with lambdas), so I never realized this before. Almost Always use Auto? Pffft.
Today I was initializing an array of SSE constants (which is annoying enough thanks to Intel intrinsics). Doing so, it occurred to me that actually auto
reads much nicer than __m128i
and is much easier to type, too. And seeing how it's absolutely certain what the _mm_set...
functions return, it should be trivially deducable, too. No risk of getting some undesirable promotion stuff either, looks like the perfect thing to do. Just works (TM).
Thus I went to write:
const auto foo[] = { _mm_set_blahblah(...), _mm_set_blahblah(...), _mm_set_blahblah(...) };
... only to learn that the compiler doesn't accept that!
After wondering whether it could be the const
for a second (the error message being about "array of const auto"), I concluded it must be the Intel intrinsics, which are usually the source of all troubles, so why not this time. Also, you are perfectly allowed to write auto&
and I'm sure that I've written auto const&
at some point in the past. That really can't be it.
Surely, with everything else, it will work! But surprise, turns out it doesn't work with simple integers, either:
int x[] = {1,2,3}; // certainly works, what else!
auto y[] = {1,2,3}; // no good
auto z[3] = {1,2,3}; // no good
Looking over at cppreference (easier than digging through the standard), it says: "may appear in the type specifier of a variable: auto x = expr;. The type is deduced from the initializer."
OK, alright, there's admittedly no square brackets in that statement, and it admittedly doesn't explicitly say "array" anywhere, but... an array is a variable (and it doesn't say "except arrays"), and there sure is an initializer.
An array is a bit special insofar as it's an aggregate, granted, but the same is true for many struct
or class
variables, and you are good to go with these.
The elements of an array must all have the same type, so there shouldn't be an issue deducing the element type correctly (other than maybe the fact that std::initializer_list
uses the same syntax as aggregate initialization, but this isn't a problem elsewhere?). The size of the array is known from the initializer list as well, so that too couldn't really be much of a hindrance (also, the standard even requires this to work, plus, it doesn't compile with an explict size, either).
So, what is the reason why declaring an auto
array isn't allowable?