के बीच रूपांतरण boost::tuple
और std::tuple
को देखते हुए, आप उनके बीच कैसे परिवर्तित होते हैं?std :: tuple और boost :: tuple
दूसरे शब्दों में, आप निम्नलिखित दो कार्यों को कैसे कार्यान्वित करेंगे?
template <typename... T> boost::tuple<T...> asBoostTuple( std::tuple<T...> stdTuple);
template <typename... T> std::tuple<T...> asStdTuple (boost::tuple<T...> boostTuple);
एक सीधा चीज लगता है, लेकिन मुझे कोई अच्छा समाधान नहीं मिला।
मैंने क्या प्रयास किया?
मैंने इसे टेम्पलेट प्रोग्रामिंग के साथ हल करने का अंत किया। ऐसा लगता है कि यह मेरे कंक्रीट सेटअप में काम करता है, लेकिन यह सही नहीं लगता है (बहुत वर्बोज़), और मुझे यह भी यकीन नहीं है कि यह वास्तव में सभी परिस्थितियों में काम करता है (मैं बाद में उस बिंदु पर आऊंगा)।
template<int offset, typename... T>
struct CopyStdTupleHelper
{
static void copy(boost::tuple<T...> source, std::tuple<T...>& target) {
std::get<offset>(target) = std::move(boost::get<offset>(source));
CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
}
static void copy(std::tuple<T...> source, boost::tuple<T...>& target) {
boost::get<offset>(target) = std::move(std::get<offset>(source));
CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
}
};
template<typename... T>
struct CopyStdTupleHelper<-1, T...>
{
static void copy(boost::tuple<T...> source, std::tuple<T...>& target)
{ /* nothing to do (end of recursion) */ }
static void copy(std::tuple<T...> source, boost::tuple<T...>& target)
{ /* nothing to do (end of recursion) */ }
};
std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
std::tuple<T...> result;
CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(boostTuple), result);
return result;
}
boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
boost::tuple<T...> result;
CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(stdTuple), result);
return result;
}
मुझे आश्चर्य है कि अगर वहाँ एक और अधिक सुरुचिपूर्ण तरीका है:
वैसे भी, यहाँ दिलचस्प हिस्सा है। यह boost::tuple
से std::tuple
का उपयोग कर मौजूदा एपीआई को लपेटने के लिए एक बहुत ही आम ऑपरेशन की तरह लगता है।
मैं एक न्यूनतम परीक्षण उदाहरण है, जहां केवल asBoostTuple
और asStdTuple
के कार्यान्वयन याद आ रही है के साथ आप प्रदान करने की कोशिश की। हालांकि, boost::tuples::null_type
के साथ कुछ जादू के लिए, जिसे मैं पूरी तरह समझ नहीं पा रहा हूं, यह संकलन करने में विफल रहता है। यही कारण है कि मुझे यकीन नहीं है कि मेरा मौजूदा समाधान आम तौर पर लागू किया जा सकता है।
#include <tuple>
#include <boost/tuple/tuple.hpp>
template <typename... T>
boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
boost::tuple<T...> result;
// TODO: ...
return result;
}
template <typename... T>
std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
std::tuple<T...> result;
// TODO: ...
return result;
}
int main() {
boost::tuple<std::string, int, char> x = asBoostTuple(std::make_tuple("A", 1, 'x'));
// ERROR:
std::tuple<std::string, int, char> y = asStdTuple<std::string, int, char>(x);
return x == y;
}
त्रुटि संदेश है:
example.cpp:20:38: error: no viable conversion from 'tuple<[3 *
...], boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type>' to 'tuple<[3 * ...], (no
argument), (no argument), (no argument), (no argument),
(no argument), (no argument), (no argument)>'
...int, char> y = asStdTuple<std::string, int, char>(x);
मैं समझता हूँ कि बूस्ट के कार्यान्वयन variadic टेम्पलेट्स, जो null_type
व्याख्या करनी चाहिए पर आधारित नहीं है, लेकिन फिर भी मैं
यहाँ टुकड़ा है मुझे यकीन नहीं है कि संकलन त्रुटि से कैसे बचें।