3

std::function関数テンプレートの 1 つのテンプレート引数の型に依存する型のを返したいと思います。

// Return a function object whose type is directly dependent on F
template<typename F, typename Arg1, typename Arg2>
auto make_f2_call(Arg1&& arg1, Arg2&& arg2)
    -> std::function<--what-goes-here?-->
{
    return [arg1, arg2](F f) { return f(arg1, arg2); };
}

// Usage example, so that it's clearer what the function does:
...
typedef bool (*MyFPtrT)(long id, std::string const& name);
bool testfn1(long id, std::string const& name);
...
auto c2 = make_f2_call<MyFPtrT>(i, n); // std::function<bool(F)>
...
bool result = c2(&testfn1);

論理的--what-goes-here?--には、戻り値の型を返し、型のF引数を取る関数の関数シグネチャである必要がありますFが、コンパイラ (Visual Studio 2010 Express) にこの意図を伝えることができないようです。(注意:使用例では、 になりますstd::function<bool(F)>。)

(注: のバリエーションを試しましたがstd::result_of<F>::type成功しませんでした。)

これは C++0x で可能ですか?

4

1 に答える 1

3

以下は、GCC 4.5.3 と MSVC 2010 EE SP1 の両方でコンパイルされます。

auto make_f2_call(Arg1&& arg1, Arg2&& arg2)
    -> std::function< typename std::result_of<F(Arg1, Arg2)>::type (F)>
{
于 2011-08-16T15:04:20.020 に答える