Benjamin Maurer Benjamin Maurer - 2 months ago 16
C++ Question

function of generic container of specific specialization

I'm trying to write an algorithm that should work with different containers (std::vector, QVector) containing the same type:

template<class Container>
boolean findpeaks(cv::Mat &m, Container<std::pair<int, double>> &peaks) {
// do stuff
peaks.push_back(std::make_pair(1, 1.0));

return true;
}


This one gives me


'Container' is not a template


template<template<typename> class Container>


I get:


error: no matching function for call to 'findpeaks(cv::MatExpr, std::vector >&)'

...

note: template argument deduction/substitution failed:

error: wrong number of template arguments (2, should be 1)


Calling code:

cv::Mat m(data, true);
std::vector<std::pair<int, double>> peaks;

QVERIFY(daf::findpeaks(m.t(), peaks));


I've also tried something like this:

template<template< template<typename, typename> typename > class Container>



warning: ISO C++ forbids typename key in template template parameter; use -std=c++1z or -std=gnu++1z [-Wpedantic]


And some more errors...

Answer

std::vector has two template parameters.

template<  
    class T,
    class Allocator = std::allocator<T>
> class vector;

And QVector has one. You can do it with variadic template:

template<template <typename...> class Container>
bool findpeaks(cv::Mat &m, Container<std::pair<int, double>> &peaks) {
    // do stuff
    peaks.push_back(std::make_pair(1, 1.0));

    return true;
}