all3fox all3fox - 3 months ago 10
C++ Question

Make a vector of comparator functions

My problem is to test an implementation of quicksort with different comparator functions:

std::less
and
std::greater
. But I do not want to copy-paste testing code that differs just in two comparators, so I would like to put them into a vector (or maybe something else?) and iterate over them.

To simplify this post, lets say I would like to write a loop over a vector of two functions that get
0
and
1
as their arguments and output a boolean. Here is my take at that:

#include <iostream>
#include <vector>
#include <functional>

int main() {
auto fs = std::vector<>{std::less<int>{}, std::greater<int>{}};

for (auto f: fs) {
std::cout << f(0, 1) << " ";
} std::cout << std::endl;
}


My
g++ 6.1.1
compiler rightfully complains that I have not specified the template arguments for the vector. I have been trying things like
std::function<bool(int, int)>
and others with no luck.

Could you tell me how to fix this piece of code?




Update: The exact error I am getting:

% g++ -std=c++14 -Wall deleteme.cpp && ./a.out
deleteme.cpp: In function ‘int main()’:
deleteme.cpp:6:27: error: wrong number of template arguments (0, should be at least 1)
auto fs = std::vector<>{std::less<int>{}, std::greater<int>{}};
^
In file included from /usr/include/c++/6.1.1/vector:64:0,
from deleteme.cpp:2:
/usr/include/c++/6.1.1/bits/stl_vector.h:214:11: note: provided for ‘template<class _Tp, class _Alloc> class std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^~~~~~
deleteme.cpp:8:18: error: unable to deduce ‘auto&&’ from ‘fs’
for (auto f: fs) {
^~

Answer

Only from C++17, template arguments of the constructor can be used to deduce the template arguments of the type, so you will have to write std::vector<std::function<bool(int,int)>> instead of std::vector<>.

Please note that std::function has a performance overhead compared to calling functions directly, so you might want to check out variadic template arguments (and swallowing) for getting the last few percentages