Why put dozens * in front of a function pointer or a function that returns a function pointer?

I found that this code, relating to different types of function markup, unexpectedly compiles, despite seemingly invalid. How can this be compiled? Is this a bug in the compiler? I am using gcc 4.8.2 on Ubuntu 14.04.

int addInt(int n,int m)      // function
{
    return n+m;
}

int (*(*functionFactoryPtr)(int n))(int, int); // pointer

int (*(functionFactory)(int n))(int, int)   // function
{
    std::cout << "Got parameter" <<  n << std::endl;
    int (*functionPtr)(int,int) = &addInt;
    return functionPtr;
}

int main()
{
    // functionFactoryPtr = @functionFactory;
    std::cout << (******(*****functionFactory)(4))(3,6) << std::endl;  // How is this not an error?

}
+4
source share
1 answer

The lvalue function is implicitly converted to a function pointer ([conv.func]). In your example, the function is converted to a pointer before each dereference.

+3
source

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


All Articles