Why can't GCC compiler deduce one of the template parameter from std::array in alias template form

283 views Asked by At

In C++20, alias templates can have implicit deduction guides if it is applied.

Then, I have constructed a simple template alias which is ints:

template <std::size_t N>
using ints = std::array<int, N>;

but:

ints{1, 2, 3, 4}

doesn't work, and GCC said:

  • error: no matching function for call to array(int, int, int, int)
  • note: couldn't deduce template parameter N
  • note: mismatched types std::array<int, N> and int

I don't understand why it fails to compile.

And:

template <typename T>
using array_of_4 = std::array<T, 4>;

with

array_of_4{1, 2, 3, 4}

won't work either.

  • Is it because std::array's deduction guide is user-provided?
  • If the above's answer is not true, then what would be the reason?

I have found a similar question regarding this issue: How to write deduction guidelines for aliases of aggregate templates?.

This concludes that, in standard, this code should be well-formed. Hence, GCC may have different implementations that prevent this code from compiling.

1

There are 1 answers

3
eerorika On
ints{1, 2, 3, 4}

I don't understand why it fails to compile.

You didn't specify the template argument. This works:

ints<4>{1, 2, 3, 4}

array_of_4{1, 2, 3, 4}

won't work either.

Same problem. This works:

array_of_4<int>{1, 2, 3, 4}

std::array's deduction guide is user-provided?

Yes, it is.