[英]dealing with variants containing move-only types
Consider the following code:考虑以下代码:
#include <iostream>
#include <variant>
#include <memory>
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...)->overloaded<Ts...>;
struct foo {
int f;
foo(int n) : f(n) {}
};
struct bar {
std::string b;
};
using unflattened_variant = std::variant<int, std::string, std::unique_ptr<foo>, std::unique_ptr<bar>>;
using flattened_variant = std::variant<int, std::string, foo, bar>;
flattened_variant flatten(const unflattened_variant& v) {
return std::visit(
overloaded{
[](int v) -> flattened_variant {
return v;
},
[](const std::string& s) -> flattened_variant {
return s;
},
[](const std::unique_ptr<foo>& f) -> flattened_variant {
return *f;
},
[](const std::unique_ptr<bar>& b) -> flattened_variant {
return *b;
},
},
v
);
}
int main()
{
unflattened_variant uv{ std::make_unique<foo>(42) };
auto fv = flatten(uv);
std::cout << std::get<foo>(fv).f << "\n";
}
This is a toy example that illustrates a situation I am running into in real code.这是一个玩具示例,说明了我在真实代码中遇到的情况。 I want to simplify the implementation of flatten(...)
such that it is less verbose when there are more types in the variant.我想简化flatten(...)
的实现,以便在变体中有更多类型时它不那么冗长。
Basically the situation is, I have a variant that contains some simple types and some move-only types that I would like to do something with.基本上情况是,我有一个变体,其中包含一些简单的类型和一些我想做的事情。 The operation that I need to perform is the same for all simple types and the same for all the move-only types;我需要执行的操作对于所有简单类型和所有仅移动类型都相同; however, I can't think of a way of dealing with the two cases (simple or move-only) using only two visiting functions.但是,我想不出只使用两个访问函数来处理这两种情况(简单或仅移动)的方法。 eg this is illegal C++ but illustrates what I want to do例如这是非法的 C++ 但说明了我想要做什么
flattened_variant flatten(const unflattened_variant& v) {
return std::visit(
overloaded{
[](const std::unique_ptr<auto>& u_ptr) -> flattened_variant {
return *u_ptr;
},
[](auto simple_value) -> flattened_variant {
return simple_value;
},
},
v
);
}
I have dealt with situations like this in the past by using a custom variant cast, similar to the one implemented here , to cast to a variant containing just those types that need to be handled the same and then using a lambda taking an auto parameter as the visitor;过去我通过使用自定义变体转换( 类似于此处实现的转换)来处理此类情况,将其转换为仅包含需要处理相同类型的变体,然后使用 lambda 将 auto 参数作为访客; however, such a cast would not work in this case because you can't copy unique_ptrs and you can't make a variant containing references.但是,这种转换在这种情况下不起作用,因为您不能复制 unique_ptrs 并且不能制作包含引用的变体。 I suppose I could write a function that will cast to a variant of pointers but am wondering if there is an easier way.我想我可以写一个 function 将转换为指针的变体,但我想知道是否有更简单的方法。
template<template<class...>class, class> struct is_instance_of:std::false_type{};
template<template<class...>class Z, class...Ts> struct is_instance_of<Z,Z<Ts...>>:std::true_type{};
template<template<class...>class Z, class T>
constexpr bool is_instance_of_v=is_instance_of<Z,T>::value;
flattened_variant flatten(unflattened_variant const& v) {
return std::visit([](auto const& e)->flattened_variant{
using T = std::decay_t<decltype(e)>;
if constexpr (is_instance_of_v<std::unique_ptr, T>){
return *e;
else
return e;
}, v);
}
we add a trait to dispatch on, then use if constexpr to have 2 function bodies.我们添加一个特征来调度,然后使用 if constexpr 来拥有 2 个 function 主体。
In c++20 we have lots more options.在c++20中,我们有更多的选择。
[]<class T>(T const& e)->flattened_variant{
if constexpr (is_instance_of_v<std::unique_ptr, T>){
Then, going back to overloading solution, we have:然后,回到重载解决方案,我们有:
[]<class T>(std::unique_ptr<T> const&)
or或者
template<class T, template<class...>class Z>
concept instance_of=is_instance_of<Z,T>::value;
then然后
[](instance_of<std::unique_ptr> auto const& e)
or或者
[]<<instance_of<std::unique_ptr> T>(T const& e)
Prior to c++17 in c++14 we can use a dispatch helper:在c++14中的c++17之前,我们可以使用调度助手:
template<class T0, class T1>
constexpr T0&& constexpr_branch( std::true_type, T0&& t0, T1&& ) { return std::forward<T0>(t0); }
template<class T0, class T1>
constexpr T1&& constexpr_branch( std::false_type, T0&&, T1&& t1 ) { return std::forward<T1>(t1); }
flattened_variant flatten(unflattened_variant const& v) {
return std::visit([](auto const& e)->flattened_variant{
using T = std::decay_t<decltype(e)>;
return constexpr_branch(
is_instance_of<std::unique_ptr, T>,
[](auto const& e){return *e;},
[](auto const& e){return e;}
)(e);
}, v);
}
going back to c++11 (where did you get your variant?), you could make an external class:回到c++11 (你从哪里得到你的变体?),你可以制作一个外部 class:
template<class R>
struct flatten {
template<class T>
R operator()(std::unique_ptr<T> const& p)const{
return *p;
}
template<class T>
R operator()(T const& v)const{
return v;
}
};
then just do a然后做一个
return std::visit( flatten<flattened_variant>{}, v );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.