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:
- Should I be able to pass the zero in the specialisation as
int
instead ofstd::size_t
when setting the type of the second template parameter toauto
? Obviously (?) the types do not match (since the type ofstd::tuple_size_v
isstd::size_t
) but I was expecting the compiler to at least warn me about something. - 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