Files
carbon-lang/examples/interop/cpp/hello_world.carbon
T
Richard SmithandJon Ross-Perkins 054dfca685 Perform overload resolution immediately in C++ operator lookup. (#6416)
Don't attempt to defer overload resolution by creating a
`CppOverloadSet`; this was incorrect as we weren't saving the complete
clang::OverloadCandidateSet, resulting in template candidates not being
found. Moreover, saving the overload candidate set would be expensive,
as the representation is surprisingly large, and is unnecessary since
we're about to build a call.

In passing, improve the diagnostics for overload resolution failure to
use Clang's operator overload resolution messages rather than its call
overload resolution messages.

This fixes calls to templated operator overloads, which is the final
piece needed for us to successfully compile an iostream-based "Hello
world" program.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-11-24 23:01:28 +00:00

56 lines
1.6 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import Cpp library "<cstdio>";
import Cpp library "<iostream>";
import Cpp library "<string_view>";
import Cpp library "<unistd.h>";
// Demonstrate passing `char`s to a C++ function.
fn HelloPutchar() {
let message: array(char, 13) =
('H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n');
for (c: char in message) {
// TODO: u8 should probably have an implicit cast to i32.
Cpp.putchar((c as u8) as i32);
}
// TODO: Use a loop over a string literal instead. Requires IndexWith support
// for str.
//
// let message: str = "Hello world!\n";
// for (c: char in message) {
// Cpp.putchar((c as u8) as i32);
// }
}
// Demonstrate passing a null-terminated string to a C++ function.
fn HelloStdio() {
// TODO: Requires mapping from Optional(const char*) into C++.
// TODO: There should be a better way to interact with functions that expect a
// null-terminated string.
// Cpp.puts(Cpp.std.data("Hello world!\n\0"));
// TODO: Requires variadic function support.
// Cpp.printf("Hello world!\n\0");
}
// Demonstrate passing a string as a void pointer to a C++ function.
fn HelloWrite() {
// TODO: Requires mapping from Optional(const char*) into a C++ const void*.
// let s: str = "Hello world!\n";
// Cpp.write(1, Cpp.std.data(s), Cpp.std.size(s));
}
fn HelloIostreams() {
Cpp.std.cout << "Hello world!\n";
}
fn Run() {
HelloPutchar();
HelloStdio();
HelloWrite();
HelloIostreams();
}