tags:

views:

99

answers:

1

Why does this code fail?

#include <algorithm>
int main() {
  int a[10];
  enum { a_size = sizeof a / sizeof *a };
  std::fill(a, a + a_size, a_size);
}

G++ 4.1.2 and 4.4.3:

In function 'int main()':
Line 5: error: no matching function for call to 'fill(int [10], int*, main()::<anonymous enum>)'

Is this code valid C++0x?

+3  A: 

std::fill is parameterized on the type of its object argument; it does not require an argument of Iterator::value_type. So, as In silico says, C++03 can't instantiate the template with a local type.

However, in C++0x, you can use local types to instantiate templates, because they are given external linkage.

Potatoswatter