I have found a code in https://stackoverflow.com/a/36132696/3206356 and I try it. It works, but I don't fully understand what happens there.
I have duplicated code from that link below:
template <size_t N, class = std::make_index_sequence<N>>
class Vector;
template <size_t N, size_t... Is>
class Vector<N, std::index_sequence<Is...>>
{
private:
std::array<double, N> vals;
template <size_t >
using double_ = double;
public:
Vector(double_<Is>... vals)
{
...
}
};
For example, we try to use it next way:
Vector<3> a(1.0, 2.0, 3.0);
How type deduction works here?
p.s.
As I understand when the compiler sees that line, first of all, it tries to deduce types for specialization. It deduces N
as 3 and Is
as empty sequence and then fails when can't find appropriate constructor. General template is not defined, so compilers must fail here too. But what happens next?
std::make_index_sequence<3>
, because that's what the default template parameter says.Is...
can never be an empty sequence (unlessN
is0
). – n. 1.8e9-where's-my-share m.N
from the general template andN
from specialization are different. When I wrote thatN
is deduced as3
, I meant for specialization. And as I understand when we deduce types for specialization we know nothing aboutstd::make_index_sequence<N>
. Am I wrong? – akulinichVector<3>
. It goes to the main template, deduces the parameters it can, and fills arguments that have default values. This turnsVector<3>
intoVector<3, std::make_index_sequence<3>>
. Then a suitable specialisation is found for this instantiation and the parameters are deduced again. – n. 1.8e9-where's-my-share m.