Mudit Sharma
Mudit Sharma

Reputation: 71

C++ type definition unclear

In the following C++ code what does double (*) double mean? What kind of a return type is it?

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;
    }
}

Upvotes: 6

Views: 293

Answers (2)

freakish
freakish

Reputation: 56467

double (*)(double) it's a function pointer signature for a function that takes one double argument and returns double. Generally

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

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

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

would be of the signature above.

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

Upvotes: 7

Jarod42
Jarod42

Reputation: 217235

double (*)(double) is type representing a pointer on function taking double and returning double.

Upvotes: 2

Related Questions