certik / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

YAML::Node has no comparison operator #265

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It isn't possible to use YAML::Node in std::set or std::map due to a lack of 
any comparison operator.

I have a scenario where I need deserialisation of a particular node to always 
return the same instance of the deserialised class. YAML::Node implements the 
equality operator, which means I can easily check if two nodes are actually 
identical, but it has no comparison operator, so I can push them into an 
std::map.

I attempted to write my own comparison operator for the std::map, but I can't 
see any publicly accessible members or accessors to do it.

Furthermore, attempts to use the comparison operator actually work, but it 
looks like the node is implicitly converted to a bool via 
YAML_CPP_OPERATOR_BOOL(). This means the comparison operator always returns 
false.

Is there any sensible way of comparison? I'd settle for internal pointer 
comparison, but it may be more sensible to compare by location in YAML tree or 
something similar.

Original issue reported on code.google.com by sho...@gmail.com on 13 Nov 2014 at 1:51

GoogleCodeExporter commented 9 years ago
I might overload hash<> so it can be used in unordered_set and unordered_map. 
Not sure about a comparison operator, though.

The implicit bool conversion is a problem, though, you're right.

Original comment by jbe...@gmail.com on 13 Nov 2014 at 2:32

GoogleCodeExporter commented 9 years ago
Overloading hash<> would work well - you could just return the internal pointer 
as an integer given it uniquely identifies a node. Do you want me to submit a 
patch to that effect?

Original comment by sho...@gmail.com on 28 Dec 2014 at 8:14