mardi 20 décembre 2022

Template parameter type deduction fails in GCC, not in Clang

Consider the following code (Godbolt):

#include <tuple>
#include <type_traits> // std::false_type, std::true_type


template <typename t_tuple, auto t_size = std::tuple_size_v<t_tuple>>
struct test : std::false_type {};

template <typename t_tuple>
struct test<t_tuple, 0> : std::true_type {};


int main(int argc, char **argv)
{
    static_assert(test<std::tuple<>>::value);
    return 0;
}

Clang and some other compilers accept the code but in GCC and ICC the static assertion fails. Either changing the type of t_size from auto to std::size_t or casting the zero in the specialisation to std::size_t alleviates the issue. What I would like to know is:

  1. Should I be able to pass the zero in the specialisation as int instead of std::size_t when setting the type of the second template parameter to auto? Obviously (?) the types do not match (since the type of std::tuple_size_v is std::size_t) but I was expecting the compiler to at least warn me about something.
  2. Is there a best practice for using auto with non-type template parameters?

(I checked this question but it had to do with const/volatile.)

Aucun commentaire:

Enregistrer un commentaire