मैं किसी तीसरे पक्ष के फ़ंक्शन के माध्यम से किसी अन्य विधि से एक विधि का आह्वान करना चाहता हूं; लेकिन दोनों वैरिएड टेम्पलेट्स का उपयोग करें। उदाहरण के लिए:std :: bind(), variadic टेम्पलेट्स, और सही अग्रेषण को कैसे गठबंधन करें?
void third_party(int n, std::function<void(int)> f)
{
f(n);
}
struct foo
{
template <typename... Args>
void invoke(int n, Args&&... args)
{
auto bound = std::bind(&foo::invoke_impl<Args...>, this,
std::placeholders::_1, std::forward<Args>(args)...);
third_party(n, bound);
}
template <typename... Args>
void invoke_impl(int, Args&&...)
{
}
};
foo f;
f.invoke(1, 2);
समस्या है, मैं एक संकलन त्रुटि मिलती है:
/usr/include/c++/4.7/functional:1206:35: error: cannot bind ‘int’ lvalue to ‘int&&’
मैं एक लैम्ब्डा उपयोग करने की कोशिश, लेकिन maybe जीसीसी 4.8 अभी तक वाक्य रचना संभाल नहीं करता है; यहाँ है कि मैं क्या करने की कोशिश की:
auto bound = [this, &args...] (int k) { invoke_impl(k, std::foward<Args>(args)...); };
मैं निम्न त्रुटि:
error: expected ‘,’ before ‘...’ token
error: expected identifier before ‘...’ token
error: parameter packs not expanded with ‘...’:
note: ‘args’
मैं क्या समझ, संकलक, जबकि मैंने सोचा, प्रकार int&&
साथ invoke_impl
का दृष्टांत करना चाहता है से कि इस मामले में &&
का उपयोग कर वास्तविक तर्क प्रकार को संरक्षित करेगा।
मैं क्या गलत कर रहा हूं? धन्यवाद,
जीसीसी 4.8 वाक्यविन्यास को पूरी तरह से संभालता है। आपने क्या प्रयास किया –
@ArneMertz मैंने – piwi
की कोशिश की गई सिंटैक्स के साथ प्रश्न को अद्यतन किया है, ऐसा लगता है कि आपको अभी gcc में एक बग का सामना करना पड़ा है, इसे काम करना चाहिए: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41934 –