You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.value() with optional default value fails to compile #3859
### Error messages
_No response_
### Compiler and operating system
Ubuntu 22.04, Clang 14
### Library version
3.11.2
### Validation
- (haven't tested this) [ ] The bug also occurs if the latest version from the [`develop`](https://github.com/nlohmann/json/tree/develop) branch is used.
- [X] I can successfully [compile and run the unit tests](https://github.com/nlohmann/json#execute-unit-tests). **All passed**
Looks like this type is failing the detail::is_getable<> test because get<> doesn't work for it. The get<> function didn't work in 3.10.5 either, so the check on value() is now stricter than it was before.
I honestly don't know if it was intended or not. I was just looking at why it wasn't working now. I know we've had various requests for and PRs about better std::optional handling.
I too ran into this problem and would find it convenient to use value with an optional fallback instead of checking contains and parse into the std::optional, which is also more error prone.
Activity
gregmarr commentedon Dec 2, 2022
Looks like this type is failing the
detail::is_getable<>test becauseget<>doesn't work for it. Theget<>function didn't work in 3.10.5 either, so the check onvalue()is now stricter than it was before.@theodelrieu This came from 74b446f Any thoughts?
audaki commentedon Dec 2, 2022
According to your answer it seems like it was never intended to work.
Therefore we solved it with a lambda for now:
Is there maybe another intended way in the library for this pattern that I overlooked in the documentation?
audaki commentedon Dec 2, 2022
PS: Maybe add a get_opt member function that is like get but wraps every type in optional?
gregmarr commentedon Dec 5, 2022
I honestly don't know if it was intended or not. I was just looking at why it wasn't working now. I know we've had various requests for and PRs about better
std::optionalhandling.sgrzeszc commentedon Nov 13, 2023
Bumping this up. This issue has hit me hard when upgrading the version of this library from 2.x to 3.x in a project I'm trying to resurrect.
Xtreme-G commentedon Oct 18, 2024
I too ran into this problem and would find it convenient to use
valuewith an optional fallback instead of checkingcontainsand parse into thestd::optional, which is also more error prone.nlohmann commentedon Nov 30, 2024
The issue is still there even after merging #4036.
optional: use #if instead of #ifndef, fixes nlohmann#3859
optional: use #if instead of #ifndef, fixes nlohmann#3859
optional: use #if instead of #ifndef, fixes nlohmann#3859
1 remaining item
optional: use #if instead of #ifndef, fixes nlohmann#3859
nebkat commentedon Dec 11, 2025
What was the reasoning behind a37eeab in the first place? Makes using
std::optionalrather inconvenient with implicit conversions.nlohmann commentedon Jan 11, 2026
See #4742 for a starting point.
optional: use #if instead of #ifndef in test, fixes #3859
optional: use #if instead of #ifndef in test, fixes nlohmann#3859 (nl…
optional: use #if instead of #ifndef in test, fixes nlohmann#3859 (nl…
Run std::optional test under default implicit-conversions build too
Document std::optional<T> direct construction limitation (#5247)
optional: use #if instead of #ifndef in test, fixes nlohmann#3859 (nl…
Document std::optional<T> direct construction limitation (nlohmann#5247)