वास्तव में कुछ समय पहले कोड कुछ ऐसा ही किया। तो निम्न कोड का प्रयास करें:
template<unsigned N, unsigned M>
struct call_up_impl{
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator& mutator, const Tuple& args, Args&&... unpacked_args) {
call_up_impl<N-1, M>::do_call(func, mutator, args, std::get<N-1>(args), std::forward<Args>(unpacked_args)...);
}
};
template<unsigned M>
struct call_up_impl<0, M> {
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator&, const Tuple&, Args&&... unpacked_args) {
func(std::forward<Args>(unpacked_args)...);
}
};
template<unsigned M>
struct call_up_impl<M, M> {
template<class Func, class Mutator, class Tuple, class... Args>
static void do_call(const Func& func, const Mutator& mutator, const Tuple& args, Args&&... unpacked_args) {
call_up_impl<M-1, M>::do_call(func, mutator, args, mutator(std::get<M-1>(args)), std::forward<Args>(unpacked_args)...);
}
};
template<int i, typename Function, typename... Parms>
void apply(Function f, Parms... parms) {
std::tuple<Parms...> t(parms...);
call_up_impl<std::tuple_size<decltype(t)>::value, i + 1>::do_call(f, &g, t);
}
यह मेरा मूल कोड की एक त्वरित रूपांतरण है, इसलिए यह अच्छी तरह से परीक्षण नहीं किया गया है और हो सकता है नहीं यह करने के लिए नहीं इष्टतम तरीका है, लेकिन यह कम से कम (कम से कम काम करना चाहिए एक त्वरित परीक्षण के अनुसार और आप वास्तव में क्या चाहते हैं के आधार पर)। बिना टपले के ऐसा करना संभव होना चाहिए, लेकिन मुझे यह नहीं मिला है कि जी ++ के साथ संकलन करें (यह आवश्यक घोंसला वाले चरम टेम्पलेट्स की तरह प्रतीत नहीं होता है)। हालांकि करने के लिए apply
बदल रहा:
template<int i, typename Function, typename... Parms>
void apply(Function f, Parms&&... parms) {
std::tuple<Parms&&...> t(std::forward<Parms>(parms)...);
call_up_impl<std::tuple_size<decltype(t)>::value, i + 1>::do_call(f, &g, t);
}
शायद भूमि के ऊपर टपल द्वारा शुरू की सबसे अधिक से बचने जाएगा। std::get
कॉल के परिणामों की सही अग्रेषण करना बेहतर होगा, लेकिन अब मैं लिखने के लिए बहुत थक गया हूं।
पीआई math.h में एम_PI के रूप में उपलब्ध है। बाकी के बारे में निश्चित नहीं है, क्या आप कम इलिप्सिस के साथ कोड पोस्ट कर सकते हैं? –
@ हंसपैसेंट मुझे लगता है कि ओपी का अर्थ है 'i-पैरामीटर' 'pi' –
@ हंसपैसेंट सी ++ अब पूरी तरह से चला गया: आपको अब सभी कोड लिखने की आवश्यकता नहीं है! इलिप्सिस वास्तव में कोड हैं और निष्पादित किए जा रहे हैं! –