Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML::Nodes alias rather than moving when using std::vector::erase, std::iter_swap and similar methods. #1209

Open
rr-mark opened this issue Aug 7, 2023 · 3 comments

Comments

@rr-mark
Copy link

rr-mark commented Aug 7, 2023

The following unit test fails

#include "gtest/gtest.h"
#include <yaml-cpp/yaml.h>
#include <vector>

TEST(VectorErase, ProducesVectorContainingCorrectElements) {
    YAML::Node a;
    a["name"] = "a";
    YAML::Node b;
    b["name"] = "b";
    YAML::Node c;
    c["name"] = "c";
    std::vector<YAML::Node> vec{a, b, c};
    vec.erase(vec.begin());
    EXPECT_EQ(vec[0]["name"].as<std::string>(), "b");
}

When vec.erase(vec.begin()) is called, instead of vec containing names {"b", "c"}, it instead contains names {"c", "c"}.

My guess is that this is something to do with aliasing rather than copying when the vector elements are moved. This is likely an issue with the move constructor or move assignment of the Node.

I am using libcpp-yaml0.6, as I am currently limited to Ubuntu 20.04.

@rr-mark
Copy link
Author

rr-mark commented Aug 7, 2023

Some other std::vector operations fail in the same way, including

std::iter_swap(vec.begin(), vec.begin()+1);

@rr-mark
Copy link
Author

rr-mark commented Aug 9, 2023

This is likely a duplicate of #721, and that issue is probably easier to test with.

@rr-mark rr-mark changed the title YAML::Node is incompatible with std::vector::erase YAML::Nodes alias rather than moving when using std::vector::erase, std::iter_swap and similar methods. Aug 9, 2023
@rr-mark
Copy link
Author

rr-mark commented Aug 10, 2023

I think this issue will be resolved by #810

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant