/ / हाना: मैं एक प्रकार से प्रकार के tuple कैसे बना सकता हूँ? - सी ++, टेम्पलेट-मेटा-प्रोग्रामिंग, बूस्ट-हाना

हाना: मैं एक प्रकार से प्रकार के tuple कैसे बना सकता हूँ? - सी ++, टेम्पलेट-मेटा-प्रोग्रामिंग, बूस्ट-हाना

अगर मेरे पास एक संस्करण है, तो ऐसा है:

using my_variant = boost::variant<int, bool, std::string>;

क्या वेरिएंट को बूस्ट में शामिल करने के तरीकों को निकालने का एक आसान तरीका है। हाना टुपल ताकि निम्न धारण हो:

using boost::hana::type;
static_assert(std::is_same<my_tuple, boost::hana::tuple<type<int>, type<bool>, type<std::string>>>{});

उत्तर:

जवाब के लिए 5 № 1

निम्नलिखित पर काम करेगा develop (जबसे e13d826):

#include <boost/hana.hpp>
#include <boost/hana/ext/boost/mpl.hpp>
#include <boost/variant.hpp>
#include <string>
namespace hana = boost::hana;


using my_variant = boost::variant<int, bool, std::string>;

constexpr auto my_tuple = hana::to<hana::tuple_tag>(my_variant::types{});

// Note:
// In general, don"t use std::is_same to compare hana::tuple; use == in
// because it will also work if the tuple contains hana::basic_types.
static_assert(my_tuple == hana::tuple_t<int, bool, std::string>, "");

क्या e13d826 के लिए समर्थन जोड़ा गया था mpl::list; केवल mpl::vector पहले समर्थित था, और boost::variant<>::types एक है mpl::list। यही कारण है कि मेरे जवाब में कुछ समय लगा; मैं इसे लागू कर रहा था :-)।

संपादित करें

मैंने समझाया नहीं कि मैं क्यों उपयोग कर रहा हूं constexpr auto my_tuple = ... के बजाय using my_tuple = decltype(...)। खैर, इसका कारण यह है कि इस प्रकार के बारे में जानना my_tuple वास्तव में उपयोगी नहीं है, क्योंकि आप इस पर भरोसा नहीं कर सकते हैं। वास्तव में, यदि आप दस्तावेज़ीकरण को देखते हैं hana::type, यह लिखा है कि आप पर भरोसा नहीं कर सकते हैं hana::type<T> कुछ विशिष्ट होने के नाते। इसके लिए अच्छे कारण हैं, लेकिन उपयोगिता के दृष्टिकोण से इसका मतलब है कि आप इस प्रकार पर भरोसा नहीं कर सकते हैं hana::tuple<hana::type<...>, ...> कुछ भी विशिष्ट है। टाइप-स्तरीय कंप्यूटेशंस करते समय, मान-स्तरीय एन्कोडिंग (इस प्रकार auto my_tuple = ...) टाइप-स्तरीय एन्कोडिंग के लिए। यह एमपीएल और दोस्तों पर हाना की विशिष्टता है, और आपको जितना संभव हो सके इसके साथ चिपकने की कोशिश करनी चाहिए, या आप हाना को वास्तव में गुस्से में पाएंगे (क्योंकि उसमें दिमाग में लिखा नहीं गया है)।


जवाब के लिए 2 № 2

यह किसी भी हाना सुविधाओं का उपयोग नहीं करता है, लेकिन काम करना चाहिए।

पहले एक transcribe फ़ंक्शन टाइप करें जो एक टेम्पलेट लेता है, और एक अलग टेम्पलेट का एक उदाहरण, और दूसरे में प्रकारों को पहले में स्थानांतरित करता है:

template<template<class...>class To, class From>
struct transcribe;
template<template<class...>class To, class From>
using transcribe_t=typename transcribe<To,From>::type;

template<template<class...>class Z, template<class...>class Src, class...Ts>
struct transcribe<Z, Src<Ts...>> {
using type=Z<Ts...>;
};

अब, एक टेम्पलेट जो प्रकार लेता है, और हाना प्रकार के हाना ट्यूपल देता है:

template<class...Ts>
using tuple_of_types = boost::hana::tuple<boost::hana::type<Ts>...>;

और हम कर रहे हैं:

template<class Src>
using get_types_from = transcribe_t< tuple_of_types, Src >;

using result = get_types_from< my_variant >;

get_types_from ऐसा इसलिए है क्योंकि एक प्रकार का फ़ंक्शन जो मनमाने ढंग से टेम्पलेट के टेम्पलेट तर्कों को निष्कर्ष निकालता है, उपयोगी लगता है।


जवाब के लिए 2 № 3

कुछ समय पहले मैं इस तरह के रूपांतरण के लिए इसी तरह से कुछ आया था। इस विचार के वास्तविक स्रोत को खोजने में सक्षम नहीं है या यह एक सुंदर आम अभ्यास भी हो सकता है:

template<class From, template<class...> class To> struct convert_impl;

template<template<class...> class From, class... Types,
template<class...> class To>
struct convert_impl<From<Types...>, To>
{
using converted_type = To<Types...>;
};

template<class From, template<class...> class To>
using convert = typename convert_impl<From, To>::converted_type;

चूंकि, मुझे बूस्ट हाना टुपल के बारे में निश्चित नहीं है, मैं std :: tuple के साथ एक उदाहरण दिखाऊंगा

convert<boost::variant<int, bool, string>, std::tuple>