1

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) {
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
OwlCodR
  • 173
  • 2
  • 9
  • Compiled well after the questions was updated according to the duplicated question. https://godbolt.org/z/cM89vnzxa Since the question was updated, the original error message and title are nonsenses. – 273K Nov 04 '21 at 22:30
  • @S.M., I see... So, thats probably because of simplifying. I wasn't even expecting that obvious and simple changes may fix the error :) I will edit question with 'full' version of my code today – OwlCodR Nov 04 '21 at 22:44
  • @user4581301, Updated. I have double-checked this [here](https://godbolt.org/z/6E71qhGoW). I've found small complete and reproduces the error, I hope) – OwlCodR Nov 05 '21 at 18:44
  • This is a different error from the one you had before, but something we can work with. Voting to reopen. – user4581301 Nov 05 '21 at 18:50
  • https://godbolt.org/z/Kz77fr3jf you can use standart function pointers and it will work – THND Nov 05 '21 at 20:22
  • I've checked [this](https://stackoverflow.com/questions/2942426/how-do-i-specify-a-pointer-to-an-overloaded-function) 'duplicate' question and I know about standard function pointers but anyway can't make it work with `` – OwlCodR Nov 06 '21 at 09:08

0 Answers0