Interesting thing: If you remove private quick_sort()
function, program will work fine.
Stackoverflow tells me to add some details because there are too many lines of code. My idea is that private overloaded function somehow confuses the compiler. It doesn't know which of
quick_sort()
's to choose or something like this
main.cpp
#include <functional>
#include <ctime>
#include <vector>
#include <iostream>
template<template<class> class T, class V>
class Sorter {
public:
static void quick_sort(T<V>* seq, std::function<int (V, V)> comp);
static clock_t sort_time(std::function<void(T<V>*, std::function<int(V, V)>)> sort, T<V>* seq, std::function<int(V, V)> comp);
private:
// This function is overloaded and private because
// It works recursively
static void quick_sort(T<V>* seq, std::function<int (V, V)> comp, int start, int end);
};
template<template<class> class T, class V>
void Sorter<T, V>::quick_sort(T<V>* seq, std::function<int (V, V)> comp) {
quick_sort(seq, comp, 0, seq->vec.size() - 1);
}
template<template<class> class T, class V>
void Sorter<T, V>::quick_sort(T<V>* seq, std::function<int (V, V)> comp, int start, int end) {
// ...
}
template<template<class> class T, class V>
clock_t Sorter<T, V>::sort_time(std::function<void (T<V>*, std::function<int (V, V)>)> sort, T<V>* seq, std::function<int (V, V)> comp) {
// ...
return clock();
}
int comp(int num1, int num2) {
// ...
return 0;
}
template<class T>
class Sequence {
public:
std::vector<T> vec;
Sequence(){};
};
int main()
{
Sequence<int> seq;
seq.vec = {2, 6, 3, 8};
// Problem is here too
// std::function<void(Sequence<int>*, std::function<int(int, int)>)> sort = Sorter<Sequence, int>::quick_sort;
std::cout << "Sort time: " << Sorter<Sequence, int>::sort_time(Sorter<Sequence, int>::quick_sort, &seq, comp);
return 0;
}
Error
<source>:47:67: error: cannot convert '<unresolved overloaded function type>' to 'std::function<void(Sequence<int>*, std::function<int(int, int)>)>'
47 | std::cout << "Sort time: " << Sorter<Sequence, int>::sort_time(Sorter<Sequence, int>::quick_sort, &seq, comp);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>: In function 'int main()':
<source>:46:101: error: conversion from '<unresolved overloaded function type>' to non-scalar type 'std::function<void(Sequence<int>*, std::function<int(int, int)>)>' requested
46 | std::function<void(Sequence<int>*, std::function<int(int, int)>)> sort = Sorter<Sequence, int>::quick_sort;
| ^~~~~~~~~~
<source>:47:67: error: cannot convert '<unresolved overloaded function type>' to 'std::function<void(Sequence<int>*, std::function<int(int, int)>)>'
47 | std::cout << "Sort time: " << Sorter<Sequence, int>::sort_time(Sorter<Sequence, int>::quick_sort, &seq, comp);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:23:88: note: initializing argument 1 of 'static clock_t Sorter<T, V>::sort_time(std::function<void(T<V>*, std::function<int(V, V)>)>, T<V>*, std::function<int(V, V)>) [with T = Sequence; V = int; clock_t = long int]'
23 | clock_t Sorter<T, V>::sort_time(std::function<void (T<V>*, std::function<int (V, V)>)> sort, T<V>* seq, std::function<int (V, V)> comp) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~