模板模板参数与容器和默认分配器:我可以让我的声明更紧凑吗? [英] template template parameters with container and default allocator: can I make my declaration more compact?

查看:225
本文介绍了模板模板参数与容器和默认分配器:我可以让我的声明更紧凑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看这个有趣的线索:
http://stackoverflow.com/a/16596463/2436175



我的具体情况涉及使用cv :: Point_和cv :: Rect_从opencv的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> >点数;
std :: vector< cv :: Rect_< double> >矩形
CreateRects(points,5.0,rects);

}

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



我想知道是否存在任何方式使我的声明更加紧凑,例如

解决方案

您可以添加模板参数的描述模板参数 Container_t 到您的功能模板:

 模板
&
typename T,
template
<
typename U,
typename = std :: allocator< U>
>
类Container_t
>
void CreateRects

const Container_t< cv :: Point_< T>& points,
const T value,
Container_t< cv :: Rect_< T>& rects

{

}

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

  template 
&
typename T,
template< typename ...> Container_t
>
void CreateRects

const Container_t< cv :: Point_< T>& points,
const T value,
Container_t< cv :: Rect_& T>& rects

{

}


I was looking at this interesting thread: http://stackoverflow.com/a/16596463/2436175

My specific case concerns declaring a templated function using a std container of cv::Point_ and cv::Rect_ from opencv. I want to template against:

  • the type of std container I will use
  • the basic data types to complete the definition of cv::Point_ and cv::Rect_

I ended up with the following declaration:

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) {

    }

which compiles fine with this:

void dummy() {

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

}

(I have also seen that I can also use, for example, CreateRects<double>(points,5,rects))

I was wondering if there existed any way to make my declaration more compact, e.g. without the need to specify 2 times the default allocator.

解决方案

You can add description for template parameters of template template parameter Container_t to your function template:

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
    )
{

}

Or you can use C++11 variadic templates:

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
    )
{

}

这篇关于模板模板参数与容器和默认分配器:我可以让我的声明更紧凑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆