63 lines
1.9 KiB
Plaintext
63 lines
1.9 KiB
Plaintext
When you want to define a function that can accept a function pointer as an argument, there are three ways in {cpp} to declare the parameter type:
|
|
|
|
* A function pointer: ``++void f(void (*callback)());++``
|
|
* A ``++std::function++``: ``++void f(std::function<void()> callback);++``
|
|
* A template argument: ``++template<class Callback> void f(Callback callback);++``
|
|
|
|
Using a function pointer is an inferior solution, for the following reasons:
|
|
|
|
* Only a function pointer can be passed as an argument, while the other options offer the caller more flexibility because they can take more advanced functors, such as lambdas with some captured state
|
|
* The syntax is obscure
|
|
* It typically has worse performance than the template parameter solution.
|
|
|
|
See S5213 for a discussion of how to choose between ``++std::function++`` and a template parameter.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,cpp]
|
|
----
|
|
using Criterion = bool (*)(DataPoint const&);
|
|
void filter(DataSet* data, Criterion criterion); // Noncompliant
|
|
|
|
using Callback = void (*)(EventInfo const&);
|
|
class Button {
|
|
public:
|
|
void addOnClick(Callback c) {myOnClickHandler = c;} // Noncompliant
|
|
private:
|
|
Callback myOnClickHandler;
|
|
};
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,cpp]
|
|
----
|
|
template<class Criterion>
|
|
void filter(DataSet* data, Criterion criterion); // Compliant, uses the more efficient template argument
|
|
|
|
using Callback = std::function<void(EventInfo const&)>;
|
|
class Button {
|
|
public:
|
|
void addOnClick(Callback c) {myOnClickHandler = c;} // Compliant, uses the more flexible std::function
|
|
private:
|
|
Callback myOnClickHandler;
|
|
};
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#t40-use-function-objects-to-pass-operations-to-algorithms[{cpp} Core Guidelines T.40] - Use function objects to pass operations to algorithms
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|