4 ms·
I'm a bit surprised by this snippet from the "Traversing and manipulating data" example: for (auto& elem : *arr) { // visitation helps deal with the po
by rpasquay 7y ago
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 ?
- jdashg 7y agoIt'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.