繁体   English   中英

确定参数的数量和类型以及作为函数的类型参数的返回类型

[英]Determining number and type of parameters and return type of type parameter that is a function

给定C ++ 11函数:

X f(A, B, C);

无论如何从这个功能内:

template<typename T>
void g(T t)
{
    ...
}

调用如下:

g(f);

确定:

  • f的参数数
  • f的参数i的类型
  • f的返回类型

...

template<typename F>
void g(F f)
{
    constexpr size_t n = num_params<F>::n; // 3
    return_type<F>::type x; // X
    tuple<param_types<F>::type...> a; // tuple<A, B, C>
}

当然:

template <typename R, typename ...Args>
void g(R(&f)(Args...))
{
    typedef R return_type;
    unsigned int const n_args = sizeof...(Args);

    // ...
}

用法:

int foo(char, bool);

g(foo);

我所做的工作符合您的期望,但是它仅限于定义最多一个函数调用运算符的函子(lambda满足此限制)。 它还适用于MSVC 2012 CTP。

namespace detail {
    ////////////////////////////////////////////////////////////////////////////
    //! Select between function pointer types
    ////////////////////////////////////////////////////////////////////////////
    template <typename T>
    struct callable_helper_ptr;

    //! non-member functions
    template <typename R, typename... Args>
    struct callable_helper_ptr<R (*)(Args...)> {
        typedef void                object_t;
        typedef R                   result_t;
        typedef std::tuple<Args...> args_t;
    };

    //! member functions
    template <typename R, typename O, typename... Args>
    struct callable_helper_ptr<R (O::*)(Args...)> {
        typedef O                   object_t;
        typedef R                   result_t;
        typedef std::tuple<Args...> args_t;
    };

    //! const member functions
    template <typename R, typename O, typename... Args>
    struct callable_helper_ptr<R (O::*)(Args...) const> {
        typedef O                   object_t;
        typedef R                   result_t;
        typedef std::tuple<Args...> args_t;
    };

    ////////////////////////////////////////////////////////////////////////////
    //! Select between function pointers and functors
    ////////////////////////////////////////////////////////////////////////////
    template <typename T, typename is_ptr = typename std::is_pointer<T>::type>
    struct callable_helper;

    //! specialization for functors (and lambdas)
    template <typename T>
    struct callable_helper<T, std::false_type> {
        typedef callable_helper_ptr<decltype(&T::operator())> type;
    };

    //! specialization for function pointers
    template <typename T>
    struct callable_helper<T, std::true_type> {
        typedef callable_helper_ptr<T> type;
    };
} //namespace detail

////////////////////////////////////////////////////////////////////////////////
//! defines the various details of a callable object T
////////////////////////////////////////////////////////////////////////////////
template <typename T>
struct callable_traits {
    typedef typename detail::callable_helper<T>::type::object_t object_t;
    typedef typename detail::callable_helper<T>::type::result_t result_t;
    typedef typename detail::callable_helper<T>::type::args_t   args_t;

    template <unsigned N>
    struct arg : public std::tuple_element<N, args_t> {};
};

如果有人有兴趣,我会写在写作背后的过程上: http : //bkentel.wordpress.com/2012/12/12/defining-a-traits-type-for-callable-objects/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM