Check if template type is any instantiation of std::array [duplicate]

扶醉桌前 提交于 2021-01-29 12:08:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!