微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在看这个有趣的线程:
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) {

    }

用这个编译很好:

void dummy() {

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

}

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

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

解决方法

您可以将模板模板参数Container_t的模板参数说明添加功能模板中:
template
    <
        typename T,template
            <
                typename U,typename = std::allocator<U>
            >
        class Container_t
    >
void CreateRects
    (
        const Container_t<cv::Point_<T> >& points,Container_t<cv::Rect_<T> >& rects
    )
{

}

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

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

}

原文地址:https://www.jb51.cc/c/119265.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐