4

だから私は与えられましたstd::tuple<T...>、そして私は受け入れる関数ポインタを作りたいですT...、現在これは私が持っているものです;

template<typename... Arguments>
using FunctionPointer = void (*)(Arguments...);

using FunctionPtr = FunctionPointer<typename std::tuple_element<0, V>::type,
                                    typename std::tuple_element<1, V>::type,
                                    typename std::tuple_element<2, V>::type>;

ただし、からすべてのインデックスを手動で入力しないと、これを行う方法を見つけることができないようです0, ..., tuple_size<V>::value。FunctionPtrは、コンテキストで定義されます。ここでV=std::tuple<T...>(また、可変個引数テンプレートがすでに存在します(したがって、直接渡すことはできませんT...))

インデックスのリストを生成し、黒魔術を行う必要があると思います。

4

2 に答える 2

5

考えられる解決策は次のとおりです。

#include <tuple>

// This is what you already have...
template<typename... Arguments>
using FunctionPointer = void (*)(Arguments...);

// Some new machinery the end user does not need to no about
namespace detail
{
    template<typename>
    struct from_tuple { };

    template<typename... Ts>
    struct from_tuple<std::tuple<Ts...>>
    {
        using FunctionPtr = FunctionPointer<Ts...>;
    };
}

//=================================================================
// This is how your original alias template ends up being rewritten
//=================================================================
template<typename T>
using FunctionPtr = typename detail::from_tuple<T>::FunctionPtr;

そして、これがあなたがそれをどのように使うかです:

// Some function to test if the alias template works correctly
void foo(int, double, bool) { }

int main()
{
    // Given a tuple type...
    using my_tuple = std::tuple<int, double, bool>;

    // Retrieve the associated function pointer type...
    using my_fxn_ptr = FunctionPtr<my_tuple>; // <== This should be what you want

    // And verify the function pointer type is correct!
    my_fxn_ptr ptr = &foo;
}
于 2013-03-07T11:34:10.690 に答える
5

単純な特性でうまくいくかもしれません:

#include <tuple>

template <typename> struct tuple_to_function;

template <typename ...Args>
struct tuple_to_function<std::tuple<Args...>>
{
    typedef void (*type)(Args...);
};

使用法:

typedef std::tuple<Foo, Bar, int> myTuple;

tuple_to_function<myTuple>::type fp; // is a void (*)(Foo, Bar, int)
于 2013-03-07T11:37:33.503 に答える