Function Returning Itself

后端 未结 9 667
鱼传尺愫
鱼传尺愫 2020-11-27 04:59

Is it possible to declare some function type func_t which returns that type, func_t?

In other words, is it possible for a function to retur

相关标签:
9条回答
  • 2020-11-27 05:12

    Assume the function definition

    T f(void)
    {
      return &f;
    }
    

    f() returns a value of type T, but the type of the expression &f is "pointer to function returning T". It doesn't matter what T is, the expression &f will always be of a different, incompatible type T (*)(void). Even if T is a pointer-to-function type such as Q (*)(void), the expression &f will wind up being "pointer-to-function-returning-pointer-to-function", or Q (*(*)(void))(void).

    If T is an integral type that's large enough to hold a function pointer value and conversion from T (*)(void) to T and back to T (*)(void) is meaningful on your platform, you might be able to get away with something like

    T f(void)
    { 
      return (T) &f;
    }
    

    but I can think of at least a couple of situations where that won't work at all. And honestly, its utility would be extremely limited compared to using something like a lookup table.

    C just wasn't designed to treat functions like any other data item, and pointers to functions aren't interchangeable with pointers to object types.

    0 讨论(0)
  • 2020-11-27 05:12

    There's a way, you just try this:

    typedef void *(*FuncPtr)();
    
    void *f() { return f; }
    
    int main() {
        FuncPtr f1 = f();
        FuncPtr f2 = f1();
        FuncPtr f3 = f2();
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 05:27

    A possible solution with structs:

    struct func_wrap
    {
        struct func_wrap (*func)(void);
    };
    
    struct func_wrap func_test(void)
    {
        struct func_wrap self;
    
        self.func = func_test;
        return self;
    }
    

    Compiling with gcc -Wall gave no warnings, but I'm not sure if this is 100% portable.

    0 讨论(0)
  • 2020-11-27 05:28
    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    typedef void *(*fptr)(int *);
    void *start (int *);
    void *stop (int *);
    void *start (int *a) {
             printf("%s\n", __func__);
             return stop(a);
    }
    void *stop (int *a) {
             printf("%s\n", __func__);
             return start(a);
    }
    int main (void) {
             int a = 10;
             fptr f = start;
             f(&a);
             return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 05:28

    If you were using C++, you could create a State object type (presuming the state machine example usage) wherein you declare an operator() that returns a State object type by reference or pointer. You can then define each state as a derived class of State that returns each appropriate other derived types from its implementation of operator().

    0 讨论(0)
  • 2020-11-27 05:29

    what about something like this:

    typedef void* (*takesDoubleReturnsVoidPtr)(double);
    
    void* functionB(double d)
    {
        printf("here is a function %f",d);
        return NULL;
    }
    
    takesDoubleReturnsVoidPtr functionA()
    {
        return functionB;
    }
    
    int main(int argc, const char * argv[])
    {
        takesDoubleReturnsVoidPtr func = functionA();
        func(56.7);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题