如果仅使用模板,则启用模板功能

Vuw*_*wox 1 c++ templates c++11

基于以下模板化的结构,用作像素访问器。

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, ...)
Run Code Online (Sandbox Code Playgroud)

我做了以下函数,仅适用于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();
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到以下或类似的事情?我想启用所有3通道像素,那是什么类型?

template<typename T, typename = typename std::enable_if<std::is_base_of< PixelAcessor<std::any, 3>,T>::value>::type>
Run Code Online (Sandbox Code Playgroud)

我想要一个C ++ 11解决方案,但是如果需要更新的编译器,我可以接受解决方案并查看我的工作。

Nat*_*ica 5

为了得到采取任何功能PixelAccessor那里N3,那么你不需要做enable_if和SFINAE。只是使用

template<typename T>
return_type function_name(PixelAccessor<T, 3> parameter_name)
{
    // stuff
}
Run Code Online (Sandbox Code Playgroud)

将为您提供一个函数,该函数可以采用PixelAccessor任何类型,大小为3。