W.F. W.F. - 11 months ago 63
C++ Question

Will template parameter deduction for constructors available since C++17 allow explicitly specify some of the class template arguments?

Apart from the most obvious usage of template parameter deduction for constructors (example available here) I can imagine some more complex use cases where we deduce only part of the parameters of the template class e.g.:

std::pair<int> p(1, 2); // std::pair<int, int>

Although this construct would be natural consequence of the deduction of template parameters in functions I couldn't find any example of this usage.
Maybe it's because of the ambiguity in case of classes with variadic template arguments?

std::tuple<int> t(1, 2, 3); // std::tuple<int, int, int>

However this way introduced syntax wouldn't substitute perfectly "make_*" wrappers (cf. N3602) where the functionality is available for us...

Answer Source

As stated by P0091:

We propose to allow a template name referring to a class template as a simple-type-specifier or with partially supplied explicit template arguments...

Emphasis added. So yes, it's allowed.

However, something should be noted:

std::pair<int> p(1, 2); // std::pair<int, int>

This deduces as pair<int, int>. But if you had supplied (1, 2.0f), it would be deduced as pair<int, float>, since the second parameter is deduced by the type supplied. This works exactly like regular template function deduction (outside of the ability to supply deduction guides).