News for hackers. Stuff that matters.
Re:TOML for Modern C++ by rpasquay on Friday February 28, 2020 @02:42AM (#22441190)
I'm a bit surprised by this snippet from the "Traversing and manipulating data" example:

  for (auto& elem : *arr) {
      // visitation helps deal with the polymorphic nature of TOML data
      elem.visit([=](auto&& el) noexcept
      {
          if constexpr (toml::is_number<decltype(el)>)
              (*el)++;
          else if constexpr (toml::is_string<decltype(el)>)
              el = "five"sv;
      });
  }
I thought constexpr would work at compile time but how can the compile here inside a loop "know" when this labmda will encounter an int or a string ?
Re:TOML for Modern C++ by jdashg on Friday February 28, 2020 @12:30PM (#22444455)
It's tricky but I think visit is being passed a generic (templated) lambda, which is statically instantiated for each type that a node might be. (Once for int, once for string, etc) Since the type is static per template instantiation, constexpr works here.
Re:TOML for Modern C++ by rpasquay on Thursday March 05, 2020 @09:53AM (#22493951)
Thank you for the explanation!
Re:TOML for Modern C++ by marzer on Friday February 28, 2020 @03:59PM (#22446316)
Yup, nailed it