Skip to content

.value() with optional default value fails to compile #3859

Closed

Description

@audaki

Description

We upgraded from 3.10.5 to 3.11.2

Since the Upgrade we can't compile value() with optional fallback value anymore

Reproduction steps

Try to compile value with std::optional<std::string>{} as fallback. Is this still possible at all, was I using a non-supported edge-case before?

Expected vs. actual results

Compiles vs not

Minimal code example

Beforehand we said:

std::optional<std::string> customer_id;
customer_id = json.value("customer_id", std::optional<std::string>{});

This no longer compiles. Even without the assignment and I tried different versions, all fail to compile:

json.value("customer_id", std::optional<std::string>{});
json.value<std::optional<std::string>>("customer_id", std::optional<std::string>{});


### 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**

Activity

  1. gregmarr commented on Dec 2, 2022

    @gregmarr
    Contributor

    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.

    @theodelrieu This came from 74b446f Any thoughts?

  2. audaki commented on Dec 2, 2022

    @audaki
    Author

    According to your answer it seems like it was never intended to work.

    Therefore we solved it with a lambda for now:

        auto json_to_opt_string = [](const nlohmann::json& v) -> std::optional<std::string> {
          if (v.empty())
            return {};
    
          return v.get<std::string>();
        };
    

    Is there maybe another intended way in the library for this pattern that I overlooked in the documentation?

  3. audaki commented on Dec 2, 2022

    @audaki
    Author

    PS: Maybe add a get_opt member function that is like get but wraps every type in optional?

  4. gregmarr commented on Dec 5, 2022

    @gregmarr
    Contributor

    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.

  5. sgrzeszc commented on Nov 13, 2023

    @sgrzeszc

    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.

  6. Xtreme-G commented on Oct 18, 2024

    @Xtreme-G

    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.

  7. nlohmann commented on Nov 30, 2024

    @nlohmann
    Owner

    The issue is still there even after merging #4036.

  8. added 3 commits that reference this issue on Dec 2, 2024
    2641464
    b20fed5
    ff7ed18
  9. 1 remaining item

  10. added and removed
    solution: proposed fixa fix for the issue has been proposed and waits for confirmation
    on Oct 23, 2025
  11. added a commit that references this issue on Dec 11, 2025
    d721591
  12. nebkat commented on Dec 11, 2025

    @nebkat
    Contributor

    What was the reasoning behind a37eeab in the first place? Makes using std::optional rather inconvenient with implicit conversions.

  13. nlohmann commented on Jan 11, 2026

    @nlohmann
    Owner

    See #4742 for a starting point.

  14. added a commit that references this issue on May 18, 2026
    6c4e4e4
  15. added this to the Release 3.12.1 milestone on May 18, 2026
  16. added a commit that references this issue on May 18, 2026
    1d7688a
  17. added a commit that references this issue on May 19, 2026
    a6ec29f
  18. added 2 commits that reference this issue on Jul 8, 2026
    ae2182c
    d0de6a9
  19. added 2 commits that reference this issue on Sep 21, 2026
    f4e3354
    5b7ede9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions