4

Is the following call to foo valid? GCC seems happy with it, while Clang gives a "no matching function" error for foo; and a note that N couldn't be inferred.

template <unsigned N>
void foo(const int (&x)[N]) {}

int main(int argc, char *argv[])
{
  foo({1,2,3});
  return 0;
}
4
  • What compiler versions are they? Commented Aug 25, 2014 at 21:06
  • GCC 4.10.0 (20140810) and Clang version 3.6.0 (trunk 215435). (I also tried on another machine with recent builds of each.) Commented Aug 25, 2014 at 21:08
  • ...does that imply that GCC should give an error? Commented Aug 25, 2014 at 21:20
  • 1
    Yes, it shouldn't be deducing an array from your initializer list argument Commented Aug 25, 2014 at 21:24

1 Answer 1

4

Edit: With the adoption of the resolution of CWG issue 1591 at the Committee's November 2014 meeting, the code in the OP is now allowed. The compiler can now deduce both the type and the number of elements in the array.


§14.8.2.5 [temp.deduct.type]/p5:

The non-deduced contexts are:

  • [...]
  • A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have std::initializer_list or reference to possibly cv-qualified std::initializer_list type.
  • [...]

Your initializer list is a non-deduced context, so clang properly rejects your code.

Conceivably, g++ could support deduction in this case as an extension. However, as it rejects this code with a "deduced conflicting values" error:

template<unsigned N>
  struct C { };

template <unsigned N>
void foo(C<N>, const int (&x)[N]) {}

int main(int argc, char *argv[])
{
  foo(C<4>(), {1,2,3});
  return 0;
}

It looks like a bug to me. (In the call to foo in the code above, the compiler is supposed to deduce N only from the first argument according to the standard, so there should be no conflict. g++ deduced N from the second argument as well, creating a conflict. This altered the behavior of a well-formed program, which isn't allowed for a conforming extension.)

Sign up to request clarification or add additional context in comments.

4 Comments

@40two Which one? The original or the modified version?
version 12.0 Update 2
Sorry didn't get it at first, the one that the OP posted. The one you posted generates the same error as well.
@40two Yeah, OK. MSVC isn't very useful for reasoning about standard conformance, anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.