Deducing a function pointer return type

前端 未结 1 1899
自闭症患者
自闭症患者 2020-12-10 04:51

I think code will better illustrate my need:

template 
struct return_type
{
  typedef ??? type;
};

so that:



        
相关标签:
1条回答
  • 2020-12-10 05:31

    If you can use variadic templates (November '12 CTP), this should work:

    template <class F>
    struct return_type;
    
    template <class R, class... A>
    struct return_type<R (*)(A...)>
    {
      typedef R type;
    };
    

    Live example.

    If you can't use variadic templates, you'll have to provide specific specialisations for 0, 1, 2, ... parameters (by hand or preprocessor-generated).

    EDIT

    As pointed out in the comments, if you want to work with variadic functions as well, you'll have to add one extra partial specialisation (or one for each parameter count in the no-variadic-templates case):

    template <class R, class... A>
    struct return_type<R (*)(A..., ...)>
    {
      typedef R type;
    };
    
    0 讨论(0)
提交回复
热议问题