Interop support for `std::getline`

Author: zygoloidCreated May 25, 2026Updated Aug 24, 2026
Labelsinactive

Meta-bug for various issues calling std::getline via C++ interop. Testcases of increasing complexity:

import Cpp library "<fstream>";
fn F(ref f: Cpp.std.istream, ref s: Cpp.std.string) -> bool {
  return not Cpp.std.getline(ref f, ref s).fail();
}
fn G(ref f: Cpp.std.istream, ref s: Cpp.std.string) -> bool {
  return Cpp.std.getline(ref f, ref s) as bool;
}
fn H(ref f: Cpp.std.ifstream, ref s: Cpp.std.string) -> bool {
  return Cpp.std.getline(ref f, ref s) as bool;
}
fn I(ref f: Cpp.std.ifstream, ref s: Cpp.std.string) {
  if (Cpp.std.getline(ref f, ref s)) {}
}

These should all work, but all fail for various different reasons. Ultimately, the goal is to be able to call getline as in I. The distinct bugs I've been able to tease apart are:

  • In the first and second call, we attempt to copy the istream (to call getline) and its base class basic_ios (to call fail). This is likely due to basic_ios being a virtual base class.
  • In the third call we additionally reject the ref f argument, claiming both that ref is present and not permitted and that it is absent and required. (#7258)
  • In the fourth call, we additionally fail to find the istream -> bool explicit conversion. Should explicit conversions to bool be used by if like they are in C++?
  • If the operator bool were non-explicit and there were no virtual inheritance, we would crash when lowering the operator bool call.

Source: carbon-language/carbon-lang