Swift file importing .hpp with std::expected with std::string does not compile

So std::expected<void, std::string> produces this compiler error:

/Applications/Xcode-16.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk/usr/include/c++/v1/__expected/expected.h:116:9 No matching constructor for initialization of 'std::__expected_void_base<std::string>::__repr'

Xcode/SourceKit's inference was clever enough to infer the type in my .swift file from calling the function that returns this -- but while clang++ compiles the C++ code fine, swift build doesn't enjoy the result.

In my understanding, creating a C++ class for the actual Result-like type I want to have is the best option. But since nobody else talked or asked about std::expected, I wanted to check how I should interpret this, and if there's another way forward.

// .h
#include <expected>
#include <string>
std::expected<void, std::string> dummy(std::string path);

// .cpp
std::expected<void, std::string> dummy(std::string path) {
  return std::unexpected{"foo"};
}

// .swift
let result = dummy(std.string("hello"))
1 Like