Skip to content

Added extra nesting level (should probably be done better) #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Added extra nesting level (should probably be done better) #266

wants to merge 2 commits into from

Conversation

haykh
Copy link

@haykh haykh commented Aug 10, 2024

the error i was trying to fix is the following. with this toml:

[A]
  ...
  [A.B1]
    ...

  [A.B2]
    ...

    [A.B2.C1]
      ...

    [A.B2.C2]
      foo = "bar"

this code doesn't compile:

toml::find_or<double>(toml_value, "A", "B2", "C2", "foo", "NONE");

while this runs ok:

toml::find<double>(toml_value, "A", "B2", "C2", "foo");
// returns "bar"

@ToruNiina
Copy link
Owner

The code:

toml::find_or<double>(toml_value, "A", "B2", "C2", "foo", "NONE");

does not compile because it requires double as the return value but got the default value "NONE" that is not convertible to double.

What you intended here is that

return double if foo exists and it is a floating-point value. otherwise return a string.

This requires C++ to change type at runtime, because the existence and the value of foo only revealed at runtime. And it is impossible because C++ determines all the types at the compile time.

That said, I confirmed that the current implementation of find_or does not compile with deeply nested tables. I also found that recursive implementation of the detail::last_one causes the problem and changed the implementation to support arbitrary depth. The current version supports much deeper find_or and I also added some test cases for that.

This patch seems to support tables one level deeper than the current, but not tables of arbitrary depth. So, I am sorry but I am closing this. I appreciate your cooperation. If you find anything else, please feel free to send it.

@ToruNiina ToruNiina closed this Aug 10, 2024
@haykh
Copy link
Author

haykh commented Aug 10, 2024

sounds good! thanks, your solution is likely way better than the hack i came up with. yes, i realized my example was not exactly accurate. but regardless, thanks for updating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants