按给定条件拆分给定的 std::variant 类型

Split a given std::variant type by a given criteria

提问人:Alexey Starinsky 提问时间:3/4/2020 最后编辑:max66Alexey Starinsky 更新时间:3/5/2020 访问量:964

问:

如何按给定的变体类型

using V = std::variant<bool, char, std::string, int, float, double, std::vector<int>>;

声明两种变体类型

using V1 = std::variant<bool, char, int, float, double>;
using V2 = std::variant<std::string, std::vector<int>>;

其中包括 的所有算术类型,包括 ?V1VV2V

V可以是模板类的参数,例如:

template <class V>
struct TheAnswer
{
    using V1 = ?;
    using V2 = ?;
};

通常,条件可以是这样的变量:constexpr

template <class T>
constexpr bool filter;
C++ C++17 标准变体

评论


答:

14赞 Barry 3/4/2020 #1

使用 Boost.Mp11,这是一个简短的单行代码(一如既往):

using V1 = mp_filter<std::is_arithmetic, V>;
using V2 = mp_remove_if<V, std::is_arithmetic>;

您还可以使用:

using V1 = mp_copy_if<V, std::is_arithmetic>;

使两者更加对称。


或者

using P = mp_partition<V, std::is_arithmetic>;
using V1 = mp_first<P>;
using V2 = mp_second<P>;

评论

0赞 Alexey Starinsky 3/4/2020
这是基于什么想法?mp_filter
0赞 Barry 3/4/2020
@AlexeyStarinsky 我不明白这个问题——你是什么意思,什么想法?
3赞 Barry 3/4/2020
@AlexeyStarinsky 阅读文档,它还链接到 Peter 写的一些帖子,内容非常丰富。
4赞 Barry 3/4/2020
@MaximEgorushkin 这是最好的元编程库 imo。我在这里有很多答案,以“使用 Boost.Mp11,这是一个简短的单行”开头
1赞 Maxim Egorushkin 3/4/2020
@Barry我现在正在阅读文档,它看起来比提升要好得多。MPL。
2赞 max66 3/4/2020 #2

编辑鉴于空变体 () 格式不正确(根据 cppreference)并且应该使用它,我修改了答案(添加了空元组的专用化)以支持 or 的类型列表为空的情况。std::variant<>std::variant<std::monostate>tuple2variant()V1V2


这有点谵妄,但是......如果声明一个帮助程序过滤器,则按如下方式对函数进行筛选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::arithmeticfilterArithm()FfilterType())

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>()... ))>()));
 };

和宣言也采取TAstd::is_arithmetic

using TA = TheAnswer<std::variant<bool, char, std::string, int, float,
                                  double, std::vector<int>>,
                     std::is_arithmetic>;

下面是一个完整的编译示例,其中 as 参数和空大小写std::is_arithmeticV2

#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> );
 }

评论

0赞 max66 3/5/2020
@xskxzr - 对不起,但我不明白你的反对意见。,据我所知,被禁止在 .voidstd::variant
1赞 xskxzr 3/5/2020
我的坏,我没有意识到格式不正确,但如果它的定义没有被实例化,似乎 std::variant<> 是可以的。std::variant<void>
6赞 Quentin 3/4/2020 #3

如果出于某种原因您不想使用 Barry 简短而合理的答案,这里有一个两者都不是(感谢@xskxzr 删除了尴尬的“引导”专业化,并感谢@max66 警告我注意空的变体极端情况):

namespace detail {
    template <class V>
    struct convert_empty_variant {
        using type = V;
    };

    template <>
    struct convert_empty_variant<std::variant<>> {
        using type = std::variant<std::monostate>;
    };

    template <class V>
    using convert_empty_variant_t = typename convert_empty_variant<V>::type;

    template <class V1, class V2, template <class> class Predicate, class V>
    struct split_variant;

    template <class V1, class V2, template <class> class Predicate>
    struct split_variant<V1, V2, Predicate, std::variant<>> {
        using matching = convert_empty_variant_t<V1>;
        using non_matching = convert_empty_variant_t<V2>;
    };

    template <class... V1s, class... V2s, template <class> class Predicate, class Head, class... Tail>
    struct split_variant<std::variant<V1s...>, std::variant<V2s...>, Predicate, std::variant<Head, Tail...>>
    : std::conditional_t<
        Predicate<Head>::value,
        split_variant<std::variant<V1s..., Head>, std::variant<V2s...>, Predicate, std::variant<Tail...>>,
        split_variant<std::variant<V1s...>, std::variant<V2s..., Head>, Predicate, std::variant<Tail...>>
    > { };
}

template <class V, template <class> class Predicate>
using split_variant = detail::split_variant<std::variant<>, std::variant<>, Predicate, V>;

在 Wandbox 上观看直播

评论

0赞 xskxzr 3/5/2020
也许你可以像这样直接打开里面的包装?Types...std::variant
0赞 max66 3/5/2020
对不起,但是......据我所知,空是格式不正确的。std::variant
0赞 Quentin 3/5/2020
@max66 显然,只有实例化是格式不正确的,所以我很清楚。我会调整它,以便回退到虽然。std::variant<>V1V2std::variant<std::monostate>
0赞 max66 3/5/2020
啊。。。仅当实例化时才格式错误...BKV型在我看来是合理的。