Home >Backend Development >C++ >How to create and use C++ generic function pointers?

How to create and use C++ generic function pointers?

PHPz
PHPzOriginal
2024-04-17 14:06:02424browse

A generic function pointer is a pointer in C that points to functions of different types and number of parameters. Creating a generic function pointer requires the use of a template, which specifies the function's return value type and a tuple of parameter types. Generic function pointers can be used with the following syntax: declare a function pointer, assign a function to the function pointer, and call the function pointed to by the function pointer. In practical cases, generic function pointers are used to implement comparison functions of sorting algorithms, which can sort elements of different types.

如何创建和使用 C++ 泛型函数指针?

How to create and use C generic function pointers

In C, a function pointer is a pointer to a function. Generic function pointers can point to functions of different types and number of parameters.

Create a generic function pointer

To create a generic function pointer, you can use the template:

template <typename Ret, typename... Args>
using GenericFunctionPointer = Ret (*)(Args...);

Where:

  • Ret is the return value type of the function.
  • Args is a tuple of parameter types for the function.

For example, create a generic function pointer to a function that receives an integer value and returns a string:

using FuncPtr = std::string (*)(int);

Using a generic function pointer

You can use the following syntax to use generic function pointers:

FuncPtr funcPtr;  // 声明函数指针
// 将函数赋值给函数指针
funcPtr = [](int x) { return std::to_string(x); };
// 调用通过函数指针指向的函数
std::string result = funcPtr(42);

Practical case

Consider a sorting algorithm that can sort elements of different types (such as integers or string) to sort. We can use a generic function pointer to implement a comparison function that compares two elements and returns an integer, depending on the size relationship of the elements.

template <typename T>
int compare(const T& a, const T& b) {
  // 比较两个元素并返回 -1 (a < b)、0 (a == b)或 1 (a > b)
  // ...
}

template <typename T>
void sort(T* array, int length, GenericFunctionPointer<int, const T&, const T&> compareFunc) {
  // 使用比较函数对数组进行排序
  // ...
}

Using this implementation, we can sort an array of integers like this:

int arr[] = {3, 1, 4, 2};
sort(arr, 4, compare<int>);

Or sort an array of strings:

std::string arr[] = {"apple", "banana", "cherry"};
sort(arr, 3, compare<std::string>);

The above is the detailed content of How to create and use C++ generic function pointers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more