c++ - Function with "auto" or template type deduction: "no matching function for call to..." -


suppose have function:

void mergesort(auto first, auto last) {     if(last - first > 1) {         auto middle = first + (last - first) /  2;         merge_sort(first, middle);         merge_sort(middle, last);         std::inplace_merge(first, middle, last);     } } 

and want use mergesort argument function (also returning void).

after reading other questions, have tried following:

void sort(auto args, std::function<void (auto, auto)> sorter) {     // stuff     sorter(args.l.begin(), args.l.end());     // other stuff } // called by: sort(args, mergesort); 

and also.

void sort(auto args, void (*sorter)(auto, auto)) {     sorter(args.l.begin(), args.l.end()); } // called by: sort(args, mergesort); 

the above attempt tried changing around pointers, etc. in case forgetting anything.

none of these work, , return error:

no matching function call 'sort(p_args<long double>&, <unresolved overloaded function type>)' 

the args argument templated struct, , works fine, having trouble passing function mergesort.

how can fix error?

using auto function parameter type not allowed c++ standard. (it possible in versions of concepts proposal, version of concepts accepted while c++2a not permit either.)

i believe g++ has support using auto function parameter type compiler extension. convert function function template, each auto replaced template type parameter. applies actual function declarations , function definitions. creating pointer function void (*)(auto, auto) or std::function<void(auto, auto)> not work compiler extension, because in both cases need specific function type, not sort of template.

so should change code explicitly use templates portable:

template <typename iter> void mergesort(iter first, iter last) { /*...*/ }  template <typename args, typename func> void sort(args& args, func&& sorter) {     std::forward<func>(sorter)(args.l.begin(), args.l.end()); } 

(note i'm assuming you'd okay forcing same deduced type both parameters of mergesort, although believe g++ treat original void (auto, auto) declaration 2 independent types.)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -