Determining the Parameter Types of an Undefined Function

前端 未结 1 1055
迷失自我
迷失自我 2020-12-11 17:24

I\'ve recently learned that I cannot:

  1. Take the address of an undefined function
  2. Take the address of a templatized function with a type it would fail t
1条回答
  •  轮回少年
    2020-12-11 18:04

    For non-overloaded functions, pointers to functions, and pointers to member functions, simply doing decltype(function) gives you the type of the function in an unevaluated context, and that type contains all the arguments.

    So to get the the argument types as a tuple, all you need are a lot of specializations:

    // primary for function objects
    template 
    struct function_args
    : function_args
    { };
    
    // normal function
    template 
    struct function_args {
        using type = std::tuple;
    };
    
    // pointer to non-cv-qualified, non-ref-qualified, non-variadic member function
    template 
    struct function_args
    : function_args
    { };
    
    // + a few dozen more in C++14
    // + a few dozen more on top of that with noexcept being part of the type system in C++17
    

    With that:

    template 
    using decltypeargs = typename function_args::type;
    

    This requires you to write decltypeargs.


    With C++17, we will have template , so the above can be:

    template 
    using decltypeargs = typename function_args::type;
    

    and you'd get the decltypeargs syntax.

    0 讨论(0)
提交回复
热议问题