Files
carbon-lang/examples/interop/cpp/hello_world.carbon
T
Richard Smith ec8c999bb1 Support passing Carbon Optional(T*) to C++ T* parameter. (#6422)
We already did this translation in the other direction, but we had no
mapping from `Optional(T)` to anything, so round-tripping a nullable
pointer from C++ through Carbon and back to C++ was previously rejected.
2025-11-25 22:30:14 +00:00

55 lines
1.5 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: There should be a better way to interact with functions that expect a
// null-terminated string.
Cpp.puts(Cpp.std.data("Hello world!\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 conversion from `const char*` to `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();
}