Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function of generic container of specific specialization [duplicate]

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...

like image 926
Benjamin Maurer Avatar asked Dec 30 '25 21:12

Benjamin Maurer


2 Answers

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;
}
like image 71
songyuanyao Avatar answered Jan 02 '26 11:01

songyuanyao


Do you actually need Container to be a class template? Just make it a normal type:

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

    return true;
}

This will let you use other containers that are potentially not templates. Like, struct MySpecialPairContainer { ... };

like image 33
Barry Avatar answered Jan 02 '26 12:01

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!