16

In C++0x (ohh! read C++11), we have automatic type inference. One thing which made me curious was that I can't create an array of auto variables. For example:

auto A[] = {1, 2, 3, 4}; // Error!

Any ideas why this might have been disallowed?

Rajendra Uppal
  • 19,218
  • 15
  • 59
  • 57
  • 3
    Why does everybody want to abuse new language features. – Martin York Aug 18 '11 at 13:00
  • 3
    @Martin: I have absolutely no intent of abusing a very beautiful language such as C++. I'm just curious about the reasons (technical, ethical etc. whatsoever) behind this decision. – Rajendra Uppal Aug 18 '11 at 13:03
  • 2
    @Martin: You have to touch the surfaces to know where the walls are before starting to run blindly. – Klaim Aug 18 '11 at 13:29

2 Answers2

12

auto deduces every brace-enclosed initializer list to a std::initializer_list<T>. (See §7.1.6.4.6 including the example). Unfortunately you cannot initialize an array or even std::array from a std::initializer_list once you have obtained it, but you can use a std::vector.

#include <vector>
#include <array>
#include <initializer_list>

int main()
{
  auto x = {1,2,3};
  std::array<int, 3> foo1 = x; // won't work for whatever reason
  std::vector<int> foo2 = x; // works as expected
  return 0;
}

Of course this defeats the whole purpose of what you are trying to do.

I tried writing a work around called make_array but had to realize that this cannot ever work as the size of an initializer_list isn't part of its template arguments and so you only instantiate one make_array template for each T. This sucks.

template<typename T> 
auto make_array(const std::initializer_list<T>& x) 
     -> std::array<T, x.size()> { } // maaah

Well, apparently you can go for the variadic-template hack mentioned here How do I initialize a member array with an initializer_list?

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156
5

Because {1, 2, 3, 4} is purely a syntactic construct- it is not an expression and does not have a type. Therefore, auto cannot deduce its type from it.

akappa
  • 10,220
  • 3
  • 39
  • 56
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 2
    Such a shame, I know languages which have been able to do that for years – hamstergene Aug 18 '11 at 12:48
  • 5
    But `auto x = {1,2}` declares `x` as a `std::initializer_list`. So this is not exactly true. – pmr Aug 18 '11 at 12:49
  • 1
    @DeadMG: Ok, so what do you think about auto x = {1, 2, 3, 4};? Is it going to work or not? What value will x contain? If x is 1, how did compiler deducted its type now? – Rajendra Uppal Aug 18 '11 at 12:49
  • @pmr: You beat me to this example! :-) – Rajendra Uppal Aug 18 '11 at 12:50
  • 1
    Eugene Homyakov: Such languages wouldn't work with the same type system. I guess you're talking about, for example Python, but it really is fundamentally different. In Python {} used on the right of an assignation can only be a dictionary, while in C++ it could be any custom type. – Klaim Aug 18 '11 at 12:56
  • 1
    @pmr: I expect that the Committee forgot to add a specific rule for `auto[]`. – Puppy Aug 19 '11 at 00:05