Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector as template parameter... all hell breaks lose

Tags:

c++

I'm exploding soon... Someone please point out what is wrong in here currently:

template <typename TType, template <typename ...> class Container, class Comparer>
Container<TType>* sort(const Container<TType>& container) {
    ...
}

The problem comes when I try to call this function with std::vector as its Container parameter. I get the following errors:

main.cpp:24:34: error: no matching function for call to 'func()'
main.cpp:24:34: note: candidate is:
main.cpp:14:6: note: template<class T, template<class ...> class Container> void func()
main.cpp:14:6: note:   template argument deduction/substitution failed:

And here's how I'm trying to call it:

std::vector<int>* m(sort<int, std::vector<int>, Comparer>(m));

When I remove the template template parameter from the function it works, but not with it... I'm using the latest g++ compiler that comes with MinGW. The IDE is NetBeans 7.3 tho that shouldn't affect much. Compiler arguments are:

-std=c++11 -Wall -pedantic

Thanks for every help, - Joey

like image 984
Wrath Avatar asked Sep 14 '26 17:09

Wrath


1 Answers

You're supposed to provide a template, not a specific type created from a template. The correct call would be:

sort<int, std::vector, Comparer>(m)

Note that sort itself is providing the template arguments for Container, as in const Container<TType>&. Clearly setting Container to std::vector<int> makes no sense; you'd be asking the compiler to do something like std::vector<int><int>

like image 111
Ben Voigt Avatar answered Sep 17 '26 07:09

Ben Voigt



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!