EDIT यह देखते हुए कि एक खाली संस्करण ( std::variant<>
) बीमार है ( cppreference के अनुसार ) और इसका उपयोग किया जाना चाहिए std::variant<std::monostate>
, मैंने tuple2variant()
इस मामले का समर्थन करने के लिए उत्तर ( खाली ट्यूपल के लिए एक विशेषज्ञता जोड़ा ) को संशोधित किया है जब प्रकार की सूची के लिए V1
या V2
खाली है।
यह थोड़ा decltype()
प्रलाप है, लेकिन ... यदि आप एक सहायक फिल्टर जोड़े को इस प्रकार घोषित करते हैं
template <bool B, typename T>
constexpr std::enable_if_t<B == std::is_arithmetic_v<T>, std::tuple<T>>
filterArithm ();
template <bool B, typename T>
constexpr std::enable_if_t<B != std::is_arithmetic_v<T>, std::tuple<>>
filterArithm ();
और वैरिएंट फ़ंक्शन के लिए एक ट्यूपल (खाली ट्यूपल्स के लिए विशेषज्ञता के साथ, खाली से बचने के लिए std::variant
)
std::variant<std::monostate> tuple2variant (std::tuple<> const &);
template <typename ... Ts>
std::variant<Ts...> tuple2variant (std::tuple<Ts...> const &);
आपकी कक्षा बस (?) बन जाती है
template <typename ... Ts>
struct TheAnswer<std::variant<Ts...>>
{
using V1 = decltype(tuple2variant(std::declval<
decltype(std::tuple_cat( filterArithm<true, Ts>()... ))>()));
using V2 = decltype(tuple2variant(std::declval<
decltype(std::tuple_cat( filterArithm<false, Ts>()... ))>()));
};
यदि आप कुछ अधिक सामान्य चाहते हैं (यदि आप std::arithmetic
टेम्पलेट पैरामीटर के रूप में पास करना चाहते हैं ), तो आप filterArithm()
टेम्पलेट-टेम्पलेट फ़िल्टर पैरामीटर F
(बदला हुआ filterType()
) को पारित करने वाले फ़ंक्शन को संशोधित कर सकते हैं।
template <template <typename> class F, bool B, typename T>
constexpr std::enable_if_t<B == F<T>::value, std::tuple<T>>
filterType ();
template <template <typename> class F, bool B, typename T>
constexpr std::enable_if_t<B != F<T>::value, std::tuple<>>
filterType ();
TheAnswer
वर्ग बन
template <typename, template <typename> class>
struct TheAnswer;
template <typename ... Ts, template <typename> class F>
struct TheAnswer<std::variant<Ts...>, F>
{
using V1 = decltype(tuple2variant(std::declval<decltype(
std::tuple_cat( filterType<F, true, Ts>()... ))>()));
using V2 = decltype(tuple2variant(std::declval<decltype(
std::tuple_cat( filterType<F, false, Ts>()... ))>()));
};
और TA
घोषणा भी लेstd::is_arithmetic
using TA = TheAnswer<std::variant<bool, char, std::string, int, float,
double, std::vector<int>>,
std::is_arithmetic>;
निम्नलिखित std::is_arithmetic
पैरामीटर और एक V2
खाली मामले के साथ एक पूर्ण संकलन उदाहरण है
#include <tuple>
#include <string>
#include <vector>
#include <variant>
#include <type_traits>
std::variant<std::monostate> tuple2variant (std::tuple<> const &);
template <typename ... Ts>
std::variant<Ts...> tuple2variant (std::tuple<Ts...> const &);
template <template <typename> class F, bool B, typename T>
constexpr std::enable_if_t<B == F<T>::value, std::tuple<T>>
filterType ();
template <template <typename> class F, bool B, typename T>
constexpr std::enable_if_t<B != F<T>::value, std::tuple<>>
filterType ();
template <typename, template <typename> class>
struct TheAnswer;
template <typename ... Ts, template <typename> class F>
struct TheAnswer<std::variant<Ts...>, F>
{
using V1 = decltype(tuple2variant(std::declval<decltype(
std::tuple_cat( filterType<F, true, Ts>()... ))>()));
using V2 = decltype(tuple2variant(std::declval<decltype(
std::tuple_cat( filterType<F, false, Ts>()... ))>()));
};
int main ()
{
using TA = TheAnswer<std::variant<bool, char, std::string, int, float,
double, std::vector<int>>,
std::is_arithmetic>;
using TB = TheAnswer<std::variant<bool, char, int, float, double>,
std::is_arithmetic>;
using VA1 = std::variant<bool, char, int, float, double>;
using VA2 = std::variant<std::string, std::vector<int>>;
using VB1 = VA1;
using VB2 = std::variant<std::monostate>;
static_assert( std::is_same_v<VA1, TA::V1> );
static_assert( std::is_same_v<VA2, TA::V2> );
static_assert( std::is_same_v<VB1, TB::V1> );
static_assert( std::is_same_v<VB2, TB::V2> );
}
Types...
अंदर खोल सकते हैं ?std::variant