2013-08-31 17 views
5

द्वारा फ़ंक्शन प्रकार कैप्चर को परिभाषित करें मैं फ़ंक्शन को टाइप टाइप में कैसे बदलूं?सी ++ 11 संदर्भ

auto fn = [&] (int x) { doSomething(x, 3); } 
+0

में इस मदद http://stackoverflow.com/questions/4295432/typedef-function-pointer करता है? – Chemistpp

उत्तर

3

कैसे

के बारे में
using my_function_type = std::function<void(int)>; 
+0

वीएस2012 काम नहीं करता है! – linquize

+0

@linquize फिर 'typedef' का उपयोग करें। –

7

आप decltype उपयोग कर सकते हैं सटीक प्रकार प्राप्त करने के लिए:

auto fn = [&] (int x) { doSomething(x, 3); }; 
    using lambda_type = decltype(fn); 

लेकिन, अधिक सामान्य प्रकार आप महज एक संगत जानना चाहते हैं, तो , लैम्ब्डा को किसी अन्य फ़ंक्शन के लिए तर्क के रूप में पास करने के लिए कहें, आप std::function<void(int)> (जो के रूप में उपयोग कर सकते हैं Achim उल्लेख)।

1
typedef std::function<void(int)> my_function_type; 

वर्क्स VS2012