C ++ type definition unclear

In the following C ++ code, what does double (*) double mean? What kind of return is this?

 auto get_fun(int arg) -> double (*)(double) // same as: double (*get_fun(int))(double) { switch (arg) { case 1: return std::fabs; case 2: return std::sin; default: return std::cos; } } 
+6
source share
2 answers

double (*)(double) is the signature of a function pointer for a function that takes one double argument and returns double . At all

 X (*)(A, B, C) // any number of args 

is a pointer to a function that takes args of types (A, B, C) and returns a value of type X , for example.

 X my_func(A, B, C) { return X(); // assuming this makes sense } 

will have the signature above.

So, in your case, get_fun is a function that returns a pointer to a function.

+7
source

double (*)(double) is a type that represents a pointer to a function that takes a double and returns a double .

+2
source

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


All Articles