rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.12k stars 430 forks source link

variant_associative_view from a std::map<std::string, rttr::variant> #290

Open 1visionary opened 4 years ago

1visionary commented 4 years ago

Hello, I am trying to create a variant_associative_view from a std::map<std::string, rttr::variant>. The code (listed below, similar to an example listed here) compiles fine, however, at runtime the view.insert calls fail and the <key, value> pair is not inserted into the map.

I am not clear if this operation is not supported or am I doing something incorrectly. Any feedback would be very much appreciated. Thank you!

    std::map<std::string, rttr::variant> my_map;
    rttr::variant var = my_map;
    if (var.is_associative_container())
    {
        rttr::variant_associative_view view = var.create_associative_view();
        view.insert(rttr::variant{"four"}, rttr::variant{4}); //FAILS
        view.insert(rttr::variant{"five"}, rttr::variant{5}); //FAILS
        std::cout << view.get_size() << std::endl; //prints '0'
        for (const auto& item : view)
        {
            // remark that the key and value are stored inside a 'std::reference_wrapper'
            std::cout << "Key: " << item.first.extract_wrapped_value().to_string() << " ";
            std::cout << "Value: " << item.second.extract_wrapped_value().to_string() << std::endl;
        }
    }