Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template template parameters with container and default allocator: can I make my declaration more compact?

I was looking at this interesting thread: https://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.

like image 288
Antonio Avatar asked Dec 10 '13 14:12

Antonio


1 Answers

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

}
like image 123
Constructor Avatar answered Sep 21 '22 12:09

Constructor