带有容器和默认分配器的模板模板参数:我可以使我的声明更紧凑吗?

Ant*_*nio 7 c++ templates opencv

我正在看这个有趣的主题:https: //stackoverflow.com/a/16596463/2436175

我的具体情况涉及使用来自opencv的cv :: Point_和cv :: Rect_的std容器声明模板化函数.我想反对模板:

  • 我将使用的std容器的类型
  • 完成cv :: Point_和cv :: Rect_定义的基本数据类型

我最后得到了以下声明:

template <typename T, template <typename, typename> class Container_t>
    void CreateRects(const Container_t<cv::Point_<T>,std::allocator<cv::Point_<T> > >& points,
                     const T value,
                     Container_t<cv::Rect_<T>,std::allocator<cv::Rect_<T> > >& rects) {

    }
Run Code Online (Sandbox Code Playgroud)

用这个编译很好:

void dummy() {

const std::vector<cv::Point_<double> > points;
std::vector<cv::Rect_<double> > rects;
CreateRects(points,5.0,rects);

}
Run Code Online (Sandbox Code Playgroud)

(我也看到我也可以使用,例如,CreateRects<double>(points,5,rects))

我想知道是否存在任何方式使我的声明更紧凑,例如,无需指定默认分配器的2倍.

Con*_*tor 11

您可以将模板模板参数的模板参数说明添加Container_t到功能模板中:

template
    <
        typename T,
        template
            <
                typename U,
                typename = std::allocator<U>
            >
        class Container_t
    >
void CreateRects
    (
        const Container_t<cv::Point_<T> >& points,
        const T value,
        Container_t<cv::Rect_<T> >& rects
    )
{

}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用C++ 11可变参数模板:

template
    <
        typename T,
        template <typename...> class Container_t
    >
void CreateRects
    (
        const Container_t<cv::Point_<T>>& points,
        const T value,
        Container_t<cv::Rect_<T>>& rects
    )
{

}
Run Code Online (Sandbox Code Playgroud)