2011-08-16 28 views
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)>だろう。)

(注:私は成功せず​​のバリエーションを試してみました。)

はC++ 0xで、このことは可能ですか?

答えて

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)> 
{ 
+0

はい、これはVS2010でも動作します。ありがとう! –

関連する問題