1

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>
}

4

2 に答える 2

7

もちろん:

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);
于 2012-12-21T22:56:04.370 に答える
2

私が取り組んだことは、あなたが望むことを実行します-しかし、それは最大で1つの関数呼び出し演算子を定義するファンクターに制限されます(ラムダはこの制限に適合します)。また、MSVC2012CTPでも機能します。

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/

于 2012-12-21T23:33:15.387 に答える