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:
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
)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With