0

While using boost::property_tree to parse json, I ran into an error and found a fix, but I do not understand why this fix works, or what the error was in the first place. It looks like auto fails here - thus the title - but I might also be completely wrong ..?

I'm using C++17 with gcc 10.2.0 in cygwin.

With the following code I get error: expected primary-expression before 'int':

// const boost::property_tree:ptree elems = ...    // a json array
const auto max_elem = *std::max_element(elems.begin(), elems.end(),
  [](const auto& lhs, const auto& rhs) {
    return (lhs.second.get<int>("size") < rhs.second.get<int>("size");
  }
);

But if name the lambda's paramter types, it compiles:

// const boost::property_tree:ptree elems = ...    // a json array
using Element = boost::property_tree::ptree::value_type;
const Element max_elem = *std::max_element(elems.begin(), elems.end(),
  [](const Element& lhs, const Element& rhs) {
    return (lhs.second.get<int>("size") < rhs.second.get<int>("size");
  }
);

The error message seems to not tell me anything so this seems terrible to bugfix if I run into this again without knowing why it doesn't work. To my knowledge this should be the same code since C++14 .. why is it not?

Shiwayari
  • 315
  • 3
  • 12

0 Answers0