How to determine the type of a function parameter given the type of argument passed to it?

后端 未结 3 2231
借酒劲吻你
借酒劲吻你 2021-02-19 11:31

I need a type trait which will report the type of a functor\'s operator() parameter given the type of the functor and the type of an argument passed to it. Basical

3条回答
  •  既然无缘
    2021-02-19 12:17

    To get started I would go with this:

    template
    struct parameter_type_impl;
    
    // may be with variadic arguments
    template
    struct parameter_type_impl {
      typedef A type;
    };
    
    template
    struct parameter_type {
      typedef typename parameter_type_impl::type type;
    };
    

    I don't see why you would pass in the actual argument type. If the conversion is not able to take place you have to use special measures (e.g. SFINAE) later on. I think the two things are orthogonal: deducing the argument type, then deciding if the argument you would like to pass in is convertible.

    The non-C++03 decltype is hard to get rid of. Specifying a function type always requires knowledge of the arguments. As soon as you would spell out the arguments, the whole thing would be moot.

    The same problem would occur with Boost.Function Types.

提交回复
热议问题