简体   繁体   English

如何制作指向模板函数的指针?

[英]How can i make pointer to a template function?

I have a template sort function that takes pointer to another function as a paramater to define sorting rule.我有一个模板排序函数,它将指向另一个函数的指针作为参数来定义排序规则。

template<typename T>
void sort(T* arr, int size, function<bool(T, T)>& comparer);

and i want to call this function with lambda expression我想用 lambda 表达式调用这个函数

sort(arr, size, [](int num1, int num2) { if (num1 > num2) { return true; } else { return false; } });

However i had an error:但是我有一个错误:

Error (active) E0304 no instance of overloaded function "sort" matches the argument list错误(活动)E0304 没有重载函数“sort”的实例与参数列表匹配

Is there a way to create pointer to template function?有没有办法创建指向模板函数的指针? How can i do this?我怎样才能做到这一点?

Template argument deduction will try to deduce T from both the first parameter T* arr and the third one function<bool(T, T)>& comparer .模板参数推导将尝试从第一个参数T* arr和第三个function<bool(T, T)>& comparer推导出T

This will fail, because the lambda doesn't actually have type function<bool(T, T)> for any T .这将失败,因为 lambda 实际上没有任何T类型function<bool(T, T)> (It can only be converted to one.) (它只能转换为一个。)

In function templates function objects are usually taken directly by a template parameter, not as std::function :在函数模板中,函数对象通常由模板参数直接获取,而不是作为std::function

template<typename T, typename F>
void sort(T* arr, int size, F comparer);

or或者

template<typename T, typename F>
void sort(T* arr, int size, F&& comparer);

depending on whether you want a copy of the function object or just a reference.取决于您是想要函数对象的副本还是只是一个引用。

  1. You probably want to take a const reference.你可能想要一个 const 引用。
  2. You have convert to std::function explicitly.您已明确转换为std::function
#include <functional>

template <typename T>
void sort(T* arr, int size, std::function<bool(T, T)> const & comparer)
{
        // sort
}

int main()
{
    int const size = 10;
    int arr[size];
    sort(arr, size, std::function{[](int num1, int num2) {
        if (num1 > num2) {
            return true;
        } else {
            return false;
        }
    }});
}

If you can change the signature of sort , see walnut's answer .如果您可以更改sort的签名,请参阅walnut 的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用函数指针作为非类型参数制作可变参数模板函数? - how can I make variadic template function with a function pointer as non type argument? 如何将函数指针模板作为模板参数? - How can I have a function pointer template as a template parameter? 如何获取指向模板类的模板方法的函数指针 - How can I get a function pointer to a template method of a template class 如何在模板 function 中执行 function 指针? - How can I execute a function pointer inside a template function? 如何使模板函数成为另一个模板函数的参数? - How can I make a template function an argument to another template function? 如何使用成员函数指针作为模板arg实例化类 - How can I instantiate class with Member function pointer as template arg 如何将成员 function 指针类型传递给模板? - How can I pass a member function pointer type to a template? 如何制作引用局部变量的函数指针? - How can I make a function pointer that references a local variable? 默认情况下,如何使函数指针参数为no-op? - How can I make a function pointer parameter no-op by default? 如何制作一个指向列表迭代器的指针,以将其作为参数传递给函数? - How can I make a pointer to list iterator to pass into function as an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM