基于以下模板化结构,用作像素访问器。
template<class T, std::size_t N>
struct PixelAccessor
{
T ch[N];
};
using pxUChar_C1 = PixelAccessor<unsigned char, 1>;
using pxUChar_C3 = PixelAccessor<unsigned char, 3>;
using pxUChar_C4 = PixelAccessor<unsigned char, 4>;
using pxFloat_C1 = PixelAccessor<float, 1>;
using pxFloat_C3 = PixelAccessor<float, 3>;
using pxFloat_C4 = PixelAccessor<float, 4>;
// etc. for all other types (int, short, ushort, ...)
我使以下函数仅适用于 3 个通道的 uchar 和 float 像素。
template<typename T, typename = typename std::enable_if<std::is_same<T, pxUChar_C3>::value || std::is_same<T, pxFloat_C3>::value>::type>
bool function(Image img) {
// where getData is uchar*, and cast to T** allow 2D accessing
auto imgPtr = (T**)img->getData();
}
有没有办法做以下事情,或类似的事情?我想启用所有 3 通道像素,有什么类型?
template<typename T, typename = typename std::enable_if<std::is_base_of< PixelAcessor<std::any, 3>,T>::value>::type>
我想要一个 C++11 解决方案,但如果需要更新的编译器,我愿意接受解决方案,看看我做了什么。