Skip to content

zig c++ -std=c++23 with #include <iostream> + <expected> is broken #25455

@stackmystack

Description

@stackmystack

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

Hello,

I am using zig build to build a C++ project.

Here's a minimal example:

#include <expected>
#include <string>
#include <iostream>

std::expected<int, std::string> parse_int(const std::string& s) {
    if (s.empty()) {
        return std::unexpected("empty string");
    }
    return 42;
}

int main() {
    auto result = parse_int("42");
    std::cout << "result = " << result.value() << "\n";
    return 0;
}

Invoking zig c++:

$ zig c++ -std=c++23 main.cpp
main.cpp:7:21: error: reference to 'unexpected' is ambiguous
    7 |         return std::unexpected("empty string");
      |                     ^
/Users/firas/.local/share/mise/installs/zig/0.15.1/lib/libcxx/include/__expected/unexpected.h:60:7: note: candidate found by name lookup is 'std::__1::unexpected'
   60 | class unexpected {
      |       ^
/Users/firas/.local/share/mise/installs/zig/0.15.1/lib/libcxx/include/__exception/operations.h:24:49: note: candidate found by name lookup is 'std::unexpected'
   24 | [[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void unexpected();
      |                                                 ^
1 error generated.

The trouble comes from #include <iostream>. The moment it's removed, the compilation works.

The real source of the issue is this definition in zig/src/libs/libcxx.zig, where -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS is explicitly and unconditionally passed.

So if you do:

/tmp/bug
$ zig c++ -std=c++23 -U_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS main.cpp

It works:

/tmp/bug
$ ./a.out
result = 42

Shouldn't this be set according to the flags sent to modules? I couldn't find a reasonable explanation for why it's enabled by default.

Expected Behavior

It needs to compile normally, and pretty sure without forcing -U_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorzig ccZig as a drop-in C compiler feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions