Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A type trait for std::array

I am trying to define a type trait that I can use with static_assert to control that one of my template classes is instantiated only with std::array<T,n>. Here is my attempt:

template <typename T>
struct is_std_array : public false_type {};

template <template <typename, size_t> class T, typename V, size_t n>
struct is_std_array<std::array<V, n>> : public true_type {};

But I get the following warning from clang:

warning: class template partial specialization contains a template parameter
that cannot be deduced; this partial specialization will never be used
non-deductible template parameter 'T'

Why is 'T' not deductible? How do I fix that?

like image 284
Frank Avatar asked Jan 07 '23 12:01

Frank


1 Answers

Your syntax on specializing is wrong:

template <template <typename, size_t> class T, typename V, size_t n>

You're introducing three template parameters here: the first of which is a template template parameter named T, that takes two parameters: a type and a size_t. Since T is not referenced anywhere in your specialization clause:

struct is_std_array<std::array<V, n>> // <== no T

it's a non-deduced context. Think of the equivalently written function:

template <template <typename, size_t> class T, typename V, size_t n>
void foo(std::array<V, n> );

Here, also, T is a non-deduced context and so would have to be specified explicitly. However, you don't actually need T at all! Just V and n. What you mean is to simply introduce the two relevant parameters directly:

template <typename V, size_t n>
struct is_std_array<std::array<V, n>> : public true_type {};
like image 131
Barry Avatar answered Jan 18 '23 04:01

Barry