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) {
^~
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
As you use different types, you might use tuple, and iterate over tuple:
namespace detail {
template <typename F, typename TUPLE, std::size_t ... Is>
void run_for_each(F&& f, TUPLE&& t, std::index_sequence<Is...>)
{
const int dummy[] = {0, (f(std::get<Is>(std::forward<TUPLE>(t))), void(), 0)...};
static_cast<void>(dummy); // Avoid warning for unused variable
}
}
template <typename F, typename TUPLE>
void run_for_each(F&& f, TUPLE&& t)
{
detail::run_for_each(std::forward<F>(f),
std::forward<TUPLE>(t),
std::make_index_sequence<
std::tuple_size<std::decay_t<TUPLE>>::value>());
}
And then
int main() {
auto t = std::make_tuple(std::less<int>{}, std::greater<int>{});
run_for_each([](auto&&f) {std::cout << f(0, 1) << " ";}, t);
std::cout << std::endl;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.