C ++ functions as parameters without pointers

When looking for ways to pass functions as parameters in C ++, I find only examples that use function pointers. However, the following compilation and output is "g20" as expected in Visual Studio. Is it better to declare f as follows:

f(void (*fun)()); 

instead

 f(void fun()); 

my example:

 #include <iostream> using namespace std; int f(void fun()); void g(); int main() { cout << f(g); } void g() { cout << "g"; } int f(void fun()) { fun(); return 20; } 
+5
source share
5 answers

Instead of function pointers, you may prefer std::function<> . It can not only store function pointers, but also lambdas, binding expressions, function objects (objects with operator() ), etc. Especially iambads will make your API much more convenient to use.

 int f(std::function<void()>& fun) { fun(); return 20; } 
+4
source

Another way that is simple and does not have the cost of std:::function is to use templates.

 template <typename Function> int f(Function function) { function(); return 20; } 

Thus, the type will be inferred as any called object. It should also allow the compiler to embed the call if it can (which is not possible when using std::function ).

0
source

Both forms are equivalent. I prefer a form that explicitly shows that the parameter is a pointer. Knowing that a parameter is a pointer is important because you can pass NULL , nullptr or 0 as an argument. Your program will compile, but crashes if someone makes a call to f(0) . You always want to check if a function pointer is not a null pointer before calling pointee if you are not sure that you cannot call your function with NULL , nullptr or 0 arguments.

If you use lambdas in your project, you should use `templates. Otherwise, you can continue to use raw function pointers, but make sure you check the function pointer (if necessary)

 template <typename Function> int f(const Function& functionp) { if(functionp) functionp(); return 20; } 

Lambdas and std::function<> objects also have a bool operator, so the if(functionp) line will also work for them. It will evaluate to false for std::function<> objects that contain nullptr, and otherwise, it will evaluate to true for std::function<> and lambdas objects.

0
source

Ampersand & makes it a link, std :: decay_t makes a link a pointer.

 template <class R, class Args...> using func_ref_t = R(&)(Args...) template <class R, class Args...> using func_ptr_t = R(&)(Args...) 
0
source

According to the C (and C ++) standard, when a parameter has a function type, the compiler automatically adjusts it to the corresponding type of function pointer. Thus, both of them are the same with respect to the compiler.

Standard part C99 6.7.5.3, clause 8:

Declaring a parameter as a return type of a function should be adjusted to a pointer to the return type of the function, as in 6.3.2.1.

C ++ 03 standard, section 8.3.5, clause 3:

[...] After determining the type of each parameter, any parameter type [...] "Function returning T" is configured as [...] "a pointer to the function returns T," respectively. [...]

0
source

Source: https://habr.com/ru/post/1235682/


All Articles