My understanding about the P0091: Template argument deduction for class templates proposal was to homogenize the behavior of function templates and class templates in deduction contexts, but I think that I have misunderstood something.
If we have this class template:
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 tend to use a helper function as syntactic sugar for creating test
objects:
template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }
Which can be used as shown below:
int buffer[5];
auto a = helper<5, int>(buffer); // No deduction
auto b = helper<5>(buffer); // Type deduced
auto c = helper(buffer); // Type and size deduced
The code above compiles. I've tried the same in Wandbox using the newer compiler setup1:
int buffer[5];
test<5, int> a(buffer); // No deduction: OK.
test<5> b(buffer); // Type deduced: FAILS.
test c(buffer); // Type and size deduced: OK.
It looks like template argument deduction for class templates works only deducing all the parameters. I was expecting both behaviors (helper function and class template) to be the same; have I misunderstood something?
1The last compilers availables in Wandbox are gcc HEAD 7.0.1 201701 and clang HEAD 5.0.0 (trunk).
type_t (&data)[size]{};
an array reference? Is the{}
an initializer? Does that compile? Also, sintactic sugar sounds pretty naughty. :) – Wassail<...>
syntax). Thereforetest<5>
is not a valid deduction placeholder. – Pazpazatype_t (&data)[size]{};
is an array reference, yes. The{}
is indeed the initializer, and it compiles try it out!. About sintactic sugar what can I say... :'( english is not my mother tonghe and I do lots of mistakes! – Pharmacisttuple<int> t{5, nullptr};
be accepted. – Titanic