Assign variant<A,B,C> from variant<C,B>?

天涯浪子 提交于 2021-01-27 14:51:33

问题


Using = does not work.

I have code like this, but it is a "bit" ugly.

#include <iostream>
#include <cassert>
#include <variant>
#include <string>

using namespace std;

namespace detail {
    template<typename... L, typename... R>
    void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral_constant<int, -1>) {
    }

    template<typename... L, typename... R, int get_idx>
    void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral_constant<int, get_idx> = {}) {
        assert(rhs_idx < std::variant_size_v< variant<R...>>);
        if (get_idx == rhs_idx) {
            cout << "assigning from idx " << get_idx << endl;
            *lhs = std::get<get_idx>(rhs);
            return;
        }
        else {
            std::integral_constant<int, get_idx - 1> prev_get_idx;
            VariantAssignRec(lhs, rhs, rhs_idx, prev_get_idx);
        }
    }
}
template<typename... L, typename... R>
void VariantAssign(variant<L...>* lhs, const variant<R...>&rhs) {
    detail::VariantAssignRec(lhs, rhs, rhs.index(), std::integral_constant<int, std::variant_size_v<variant<R...>>-1>{});
}


int main()
{
   std::variant<int, char, std::string> va = 'a';
   std::variant<std::string, int> vb = string("abc");
   cout << "va index is  " << va.index() << endl; 
   cout << "vb index is  " << vb.index() << endl; 
   VariantAssign(&va, vb);
   cout << "va index now should be 2, and it is  " << va.index() << endl; 
   vb = 47;
   VariantAssign(&va, vb);
   cout << "va index now should be 0, and it is  " << va.index() << endl; 
}

I am using VS so no if constexpr but I am looking for general C++17 solution regardless of VC++ lacking support.


回答1:


Just use a visitor:

std::variant<A, B, C> dst = ...;
std::variant<B, C> src = B{};

std::visit([&dst](auto const& src) { dst = src; }, src);

If there is a type in src that isn't assignable to dst, this won't compile - which is probably the desired behavior.

If you end up using this pattern semi-often, you could move the assigner into its own function:

template <class T>
auto assignTo(T& dst) {
    return [&dst](auto const& src) { dst = src; };
}

std::visit(assignTo(dst), src);



回答2:


You may use a visitor:

struct overload_priority_low{};
struct overload_priority_high : overload_priority_low{};

template <typename V>
struct AssignTo
{
private:
    V& v; 

public:
    explicit AssignTo(V& v) : v(v) {}

    template <typename T>
    void operator () (T&& t) const
    {
        assign(std::forward<T>(t), overload_priority_high{});   
    }

private:

    template <typename T>
    auto assign(T&& t, overload_priority_high) const
    -> decltype(this->v = std::forward<T>(t), void())
    {
        v = std::forward<T>(t);
    }

    template <typename T>
    void assign(T&& t, overload_priority_low) const
    {
        throw std::runtime_error("Unsupported type");
    }

};

With usage:

int main() {
    std::variant<int, char> v = 0;
    std::variant<int, char, std::string> v2 = 42;

    std::visit(AssignTo(v), v2);
}

Demo



来源:https://stackoverflow.com/questions/44816579/assign-varianta-b-c-from-variantc-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!