Search code examples
yamlc++17clion

Why I see this CLion error when iterating over yaml_cpp node?


I have the following simple iteration over yaml-cpp node that contains a list of items:

for (const auto &group_entry: node["groups"]) { /*...*/ }

The code compiles and works as expected, but in CLion IDE it is addressed as an error, claiming "const YAML::Node' is not a valid range type". Is it any problem with this and if not, how could I remove this annoying highlight? This is a C++17 project.


Solution

  • Generally, the compiler is the better judge of whether some code is valid or not. Your code is perfectly fine.

    The error message also gives us a hint at what seems to be the problem: It talks about a range type which simply is not a thing in this context (C++20 ranges library does exist but is irrelevant here). The range-for you are using simply requires the range-expression to be either an array, have begin and end functions defined, or be a braced-init-list.

    So what we are seeing is likely the result of CLion not correctly checking this restraint, possibly by re-using code from JetBrains' other IDEs that is not appropriate for C++. The issue you created is therefore appropriate way of making this error go away.