mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Triggered by calling a binary operator with LHS being an imported C++
class type.
Not supported (yet):
* Multiple overloads.
* Other operators.
C++ Interop Demo:
```c++
// hello_world.h
class C {
public:
C(int x) : x_(x) {}
auto x() const -> int { return x_; }
private:
int x_ = 0;
};
auto operator+ (C c1, C c2) -> C;
```
```c++
// hello_world.cpp
#include "hello_world.h"
#include <cstdio>
auto operator+ (C c1, C c2) -> C {
printf("Adding %d with %d\n", c1.x(), c2.x());
return C(c1.x() + c2.x());
}
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
let c1 : Cpp.C = Cpp.C.C(7);
let c2 : Cpp.C = Cpp.C.C(8);
let c3 : Cpp.C = c1 + c2;
let c4 : Cpp.C = c3 + c2;
return 0;
}
```
```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
Adding 7 with 8
Adding 15 with 8
```
Part of #5995.