I have been playing with auto
and I noticed that for most cases you can replace a variable definition with auto
and then assign the type.
In the following code w
and x
are equivalent (default initialized int
, but lets not get into potential copies). Is there a way to declare z
such that it has the same type as y
?
int w{};
auto x = int{};
int y[5];
auto z = int[5];
decltype
works with g++ 4.9.0 20130601 for this:Output:
Not exactly the same thing, and it's a bit ugly, but it is possible to deduce the element type from a list initializer and declare the array directly, as follows:
The type should be
int[3]
and the output should be4 5 7
.Not quite the same, but you could use
array
:TL;DR
Your example of
auto z = int[5];
isn't legal any more thanauto z = int;
is, simply because a type is not a valid initializer. You can write:auto z = int{};
becauseint{}
is a valid initializer.Once one realizes this, the next attempt would be:
Note that your
int y[5]
does not have any initializer. If it had then you would have jumped straight here.Unfortunately this does not work either for obscure syntax reasons. Instead you must find a legal way to name the array type in an initializer. For example, a typedef name can be used in an initializer. A handy reusable template type alias eliminates the burdensome requirement of a new typedef for every array type:
Aside: You can use template type aliases to fix the weird 'inside-out' syntax of C++, allowing you to name any compound type in an orderly, left-to-right fashion, by using this proposal.
Unfortunately due to the design bug in C and C++ which causes array-to-pointer conversions at the drop of a hat, the deduced type of the variable
z
isint*
ratherint[5]
. The resulting variable becomes a dangling pointer when the temporary array is destroyed.C++14 introduces
decltype(auto)
which uses different type deduction rules, correctly deducing an array type:But now we run into another design bug with arrays; they do not behave as proper objects. You can't assign, copy construct, do pass by value, etc., with arrays. The above code is like saying:
By all rights this should work, but unfortunately built-in arrays behave bizarrely in C and C++. In our case, the specific problem is that arrays are not allowed to have just any kind of initializer; they are strictly limited to using initializer lists. An array temporary, initialized by an initializer list, is not itself an initializer list.
Answer 1:
At this point Johannes Schaub makes the excellent suggestion that we can use temporary lifetime extension.
decltype(auto)
isn't needed because the addition of&&
changes the deduced type, so Johannes Schaub's suggestion works in C++11. This also avoids the limitation on array initializers because we're initializing a reference instead of an array.If you want the array to deduce its length from an initializer, you can use an incomplete array type:
Although the above does what you want you may prefer to avoid raw arrays entirely, due to the fact that raw arrays do not behave like proper C++ objects, and the obscurity of their behavior and the techniques used above.
Answer 2:
The
std::array
template in C++11 does act like a proper object, including assignment, being passable by value, etc., and just generally behaving sanely and consistently where built-in arrays do not.However, with this you miss out on being able to have the array type infer its own length from an initializer. Instead You can write a
make_array
template function that does the inference. Here's a really simple version I haven't tested and which doesn't do things you might want, such as verify that all the arguments are the same type, or let you explicitly specify the type.