Template deduction guide for std::array

In the template deduction guide for std::array:

1
2
3
4
5
namespace std {
    template<typename T, typename... U> array(T, U...)
        -> array<enable_if_t<(is_same_v<T, U> && ...), T>,
                (1 + sizeof...(U))>;
}


What happens when you do std::array a{45} such that the variadic template argument is empty and T is an int? Is this deduction guide even used in that case? And if so, what is the result of is_same_v<T,U> if the variadic U argument is empty?
Last edited on
In a fold expression of length of 0, such as is_same_v<T, U> && ..., the result when passed a pack of length zero is true. So yes, the deduction guide still works even in this case, and there is no result for is_same_v<T, U> since that is never instantiated.

The overall result for your example after substitution would be something like
1
2
3
4
namespace std {
  template <>
  array(int) -> array<enable_if_t<true, int>, (1 + 0)>;
}
I thought a fold expression with an empty pack is ill formed. Is there some exception for the && operator (and maybe also the || operator)?
https://en.cppreference.com/w/cpp/language/fold

When a unary fold is used with a pack expansion of length zero, only the following operators are allowed:

1) Logical AND (&&). The value for the empty pack is true
Thank you.
Topic archived. No new replies allowed.