My idea of the C++17 feature Template Parameter Induction for Template Classes was that it was intended to homogenize the behavior of template functions and template classes in cases of parameter induction. But I think there is something I have not understood.
If we have this template object:
template <std::size_t S, typename T>
struct test
{
static constexpr auto size = S;
using type_t = T;
test(type_t (&input)[size]) : data(input) {}
type_t (&data)[size]{};
};
I usually use a helper function to make object creation easier test
:
template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }
Which can be used in the following way:
int main()
{
int buffer[5];
auto a = helper<5, int>(buffer); // Sin deduccion.
auto b = helper<5>(buffer); // Tipo deducido.
auto c = helper(buffer); // Tipo y tamanyo deducidos.
std::cout << a.size << b.size << c.size;
return 0;
}
The above code shows 555
as expected. I have tried the same thing on Wandbox using newer compilers 1 :
int main()
{
int buffer[5];
test<5, int> a(buffer); // Sin deduccion: Funciona.
test<5> b(buffer); // Tipo deducido: FALLO.
test c(buffer); // Tipo y tamanyo deducidos: Funciona.
std::cout << a.size << b.size << c.size;
return 0;
}
It seems that parameter deduction for template classes only works when all parameters are deducted, I was expecting both behaviors (per-function and per-class deduction) to be the same, is there something I misunderstood?
1 The latest compilers available on Wandbox are gcc HEAD 7.0.1 201701 and clang HEAD 5.0.0 (trunk) .
The answers shown below are taken from the same question on SOen (Please give due credit to the original authors).
Both answers go on to say that it is not clear from the whitepaper itself that partial inference of template parameters in template classes is allowed and that partial inference was dismissed as confusing. As we can see in the examples of the question the total deduction works.
Nicol Balls
There seems to be a contradiction in the Template Parameter Deduction for Template Classes whitepaper itself ; it is stated that partial specification of parameters should be allowed (translation mine, emphasis from original answer):
But no way to deal with "explicitly partially provided parameters" is provided in the standard text on the same proposal.
metalfox
Extracted from Botond Ballo 's trip 1 report:
1 Report of the trip refers to the fact that the author of the blog (Botond Ballo) participated in the meeting of the standards committee that took place in Oulu in June 2016. The author of the blog " There's Waldo! " reports the conversations that he was able to witness over there.