Is There a way to use the Parameter Names from a typedef

后端 未结 2 1402
我寻月下人不归
我寻月下人不归 2021-01-26 07:23

So given a typedef that defines a function pointer with parameter names like this:

typedef void(*FOO)(const int arg);

Is there a w

相关标签:
2条回答
  • 2021-01-26 07:45

    The definition of a function pointer has nothing to do with the declaration nor the definition of a function so the answer is no.

    0 讨论(0)
  • 2021-01-26 08:00

    What you are trying to do will not work. FOO is an alias for void(*)(const int). so

    FOO foo {
        cout << arg << endl;
    }
    

    becomes

    void(*)(const int) foo {
        cout << arg << endl;
    }   
    

    and that just doesn't work. What you can do though is define a macro that takes a name and use that to stamp out a function signature. That would look like

    #define MAKE_FUNCTION(NAME) void NAME(const int arg)
    
    MAKE_FUNCTION(foo){ std::cout << arg * 5 << "\n"; }
    
    MAKE_FUNCTION(bar){ std::cout << arg * 10 << "\n"; }
    
    int main()
    {
        foo(1);
        bar(2);
    }
    
    0 讨论(0)
提交回复
热议问题