0

C++17 has introduced class template argument deduction. While most of the time it's nothing more than a syntactic sugar, there are cases when it really comes to the rescue, especially in the generic code, like in this case.

Another nice application of this feature is a usage of std::array. Indeed, now it causes much less pain, just compare these two versions:

std::array arr{ 1, 2, 3, 4, 5 }; // with C++17 template argument deduction
std::array<int, 5> arr{ 1, 2, 3, 4, 5 }; // just a normal C++11 std::array

Indeed, once I decide to add one more element, I can just do it without explicitly changing the type to std::array<int, 6>.

However, the following doesn't compile:

std::array arr{ 1, 2, 3.f, 4, 5 };

It makes sense that the template argument deduction failed, producing an error like the following:

main.cpp:7:26: error: class template argument deduction failed:
     std::array arr{2,4.,5}; // will not compile

However, when I tried to give a hint, it didn't fix the problem:

std::array<float> arr{ 1, 2, 3.f, 4, 5 }; // will not compile as well

Now the compiler error message states the following:

main.cpp:7:21: error: wrong number of template arguments (1, should be 2)
     std::array<float> arr{2,4.,5};
                    ^

Why is that? Why can't I provide only some of the template arguments for a class, but I can for functions?

Justin
  • 24,288
  • 12
  • 92
  • 142
Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25

1 Answers1

0

C++ class templates never supported partial deduction. A similar question has recently been asked here, and the accepted answer states that partial deduction was included in the original proposal, but didn't land in the Standard.

Given that, the compiler error looks logical.

Futhermore, this situation is described on the CppReference page referenced in my question, in the section "Notes":

std::tuple t(1, 2, 3);              // OK: deduction
std::tuple<int,int,int> t(1, 2, 3); // OK: all arguments are provided
std::tuple<int> t(1, 2, 3);         // Error: partial deduction

P.S. Anyway, I noted for myself that even though this aspect is well-known and I'm well used to it, the new deduction rules somehow give rise to a confusion in my brain, so for whatever reason I start subconsciously seeing such situations as if I work with a function, expecting the same behavior as from a function template, including partial deduction.

If you have the same feeling, or don't have it at all, I would be interested to hear your thoughts in the comments.

Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
  • Given that you already found the answer to your question, why did you feel the need to post a new one? – Barry Jul 20 '17 at 23:38
  • My intent was to document my story. I assumed the checkbox "Answer your own question" is supported for this reason. At least, that's the impression I've got from [the explanation by Jeff Atwood](https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/). To quote: "Bottom line — never hesitate to ask and answer your own question on any Stack Exchange site". Did I misinterpret it? – Vasiliy Galkin Jul 21 '17 at 06:15