Adrian McCarthy
Adrian McCarthy

Reputation: 48021

Template argument deduction for array types

Microsoft VC++ 2010 gives an error on this code:

template <int D, typename T>
void Foo(T x[D]) {
  // details omitted
}

int main() {
  float x[3];
  Foo(x);  // C2784: could not deduce template argument for 'T [D]' from 'float [3]'
  return 0;
}

The same code passes muster with gcc and clang.

Is this a bug with VC++ 2010?

If it is a bug:

  1. Does anyone know if it's been fixed in a later version of VC++?
  2. Is there a workaround besides explicitly calling Foo<3, float>?

If it is not a bug:

Is there an extension to gcc and clang that allows them to resolve the template arguments?

I've greatly simplified the actual code down to this small example. I've tried it on other compilers, but I don't presently have access to newer Microsoft compilers. I've found similar questions on SO, but none that specifically address this case.

Upvotes: 4

Views: 330

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52601

A parameter of type T x[D] is equivalent to T x[] aka T* x. D cannot be deduced from it. Make it void Foo(T (&x)[D]) - you are passing a reference to an array this way.

Upvotes: 6

Related Questions