yaml-cpp
yaml-cpp copied to clipboard
Node access with YAML::Node fails where it succeds with std::string
I have a YAML::Node which I use as a map.
When accessing the map with std::string everything works fine. But if I access it with a YAML::Node which just represents the value of my previous std::string it fails.
Using doc1 one fails. I expected doc1[key1] == doc1[key2].
If I create the content of my document with key2 (see doc2), then everything works as expected.
Is this a yaml-cpp bug? Or am I overlooking something?
#include <iostream>
#include <yaml-cpp/yaml.h>
int main() {
std::string key1 = "xs";
YAML::Node key2;
key2 = key1;
// Creating yaml document with std::string access
YAML::Node doc1;
doc1[key1][0] = 0;
doc1[key1][1] = 1;
doc1[key1][2] = 2;
{
YAML::Emitter e;
e << doc1;
std::cout << "doc1:\n" << e.c_str() << "\n";
}
{
YAML::Emitter e;
e << doc1[key1];
std::cout << "only xs with key1:" << e.c_str() << "\n";
}
//This fails!
{
YAML::Emitter e;
e << doc1[key2];
std::cout << "only xs with key2:" << e.c_str() << "\n";
}
// expecting doc1[key1] == doc1[key2] which is not true
// Creating yaml document with YAML::Node access
YAML::Node doc2;
doc2[key2][0] = 0;
doc2[key2][1] = 1;
doc2[key2][2] = 2;
{
YAML::Emitter e;
e << doc2;
std::cout << "doc2:\n" << e.c_str() << "\n";
}
{
YAML::Emitter e;
e << doc2[key1];
std::cout << "only xs with key1:" << e.c_str() << "\n";
}
// Here it doesn' fail
{
YAML::Emitter e;
e << doc2[key2];
std::cout << "only xs with key2:" << e.c_str() << "\n";
}
}
It should work if you access it as a string, e.g.
doc1[key2.as<string>()]
It should work if you access it as a string, e.g.
doc1[key2.as<string>()]
Yes of course, but I don't know what key2 is. It does not have to be a std::string, and it should work with std::string.