mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
C++ in
C++ Interop Demo:
```c++
// hello_world.h
auto hello_world(bool x) -> bool;
```
```c++
// hello_world.cpp
#include "hello_world.h"
#include <cstdio>
auto hello_world(bool x) -> bool {
printf("bool: %d\n", x);
return !x;
}
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
let x: bool = Cpp.hello_world(false);
if (x) {
return 0;
} else {
return 1;
}
}
```
```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
bool: 0
```
Before this change (bool is interpreted as a 1 bit integer):
```shell
$ bazel-bin/toolchain/carbon compile main.carbon
... CRASH! ...
clang/include/clang/AST/Type.h:952: const ExtQualsTypeCommonBase *clang::QualType::getCommonPtr() const: Assertion `!isNull() && "Cannot retrieve a NULL type pointer"' failed.
```
Part of #5263.