问题
I'm in need of some compile time check if a template type passed into a templated function is any instantiation of std::array
Like
IsStdArray<std::array<float, 12>>::value; // should evaluate to true
IsStdArray<std::array<int, 1000>>::value; // should evaluate to true
IsStdArray<std::vector<float>>::value; // should evaluate to false
IsStdArray<std::string>::value // should evaluate to false
I'm especially struggling to come up with anything that is independent of the array size. Note, that a function returning a constexpr bool
would be fine as solution too!
回答1:
You can partially specialise a trait class.
template<typename T>
struct IsStdArray : std::false_type {};
template<typename T, std::size_t N>
struct IsStdArray<std::array<T, N>> : std::true_type {};
来源:https://stackoverflow.com/questions/61252904/check-if-template-type-is-any-instantiation-of-stdarray