Home > Backend Development > C++ > How Do Callbacks Work in C , and What Are Their Various Uses?

How Do Callbacks Work in C , and What Are Their Various Uses?

Mary-Kate Olsen
Release: 2024-12-14 06:20:11
Original
347 people have browsed it

How Do Callbacks Work in C  , and What Are Their Various Uses?

Callbacks and their Various Uses in C

In C , a callback is a callable (discussed later) accepted by a class or function, employed to customize current logic based on the provided callback.

Reasons for Using Callbacks:

  • To write generic code independent of logic in the called function, allowing for reuse with different callbacks.
  • To notify callers of specific events, enabling static and compile-time flexibility.

What are Callables in C ?

  • Function pointers (including pointers to member functions)
  • std::function objects
  • Lambda expressions
  • Bind expressions
  • Function objects (classes with overloaded function call operator operator())

Writing and Calling Callbacks:

Function Pointers:

  • Type Notation: return_type (*)(parameter_type_1, parameter_type_2, parameter_type_3)
  • Callback Call Notation: function_pointer(argument)
  • Callback Use Notation: function(argument_list, function_pointer)

Pointer to Member Function:

  • Type Notation: return_type (class_type::*)(parameter_type_1, parameter_type_2, parameter_type_3)
  • Callback Call Notation: (class_instance.*member_function_pointer)(argument)
  • Callback Use Notation: function(argument_list, class_instance, member_function_pointer)

std::function Objects:

  • Type Notation: std::function
  • Callback Call Notation: function_object()
  • Callback Use Notation: function(argument_list, function_object)

Examples Using std::function:

Generalizing Code:

template<class R, class T>
void stdf_transform_every_int_templ(int *v, unsigned const n, std::function<R(T)> fp) {
  for (unsigned i = 0; i < n; ++i) {
    v[i] = fp(v[i]);
  }
}
Copy after login

Templated Callbacks:

template<class F>
void transform_every_int_templ(int *v, unsigned const n, F f) {
  for (unsigned i = 0; i < n; ++i) {
    v[i] = f(v[i]);
  }
}
Copy after login

Type-Name Implementation:

template <class T>
std::string type_name() {
  typedef typename std::remove_reference<T>::type TR;
  std::unique_ptr<char, void(*)(void*)> own
    (abi::__cxa_demangle(typeid(TR).name(), nullptr,
    nullptr, nullptr), std::free);
  std::string r = own != nullptr?own.get():typeid(TR).name();
  if (std::is_const<TR>::value)
    r += " const";
  if (std::is_volatile<TR>::value)
    r += " volatile";
  if (std::is_lvalue_reference<T>::value)
    r += " &amp;&";
  else if (std::is_rvalue_reference<T>::value)
    r += " &amp;&amp;";
  return r;
}
Copy after login

The above is the detailed content of How Do Callbacks Work in C , and What Are Their Various Uses?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template