Skip to content

Commit f087bb7

Browse files
committed
book: Clean up the C++ section some more
Trying to set expectations for what we can do with C++ a little better.
1 parent 9113b7b commit f087bb7

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

book/src/cpp.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
# Generating Bindings to C++
22

3-
`bindgen` can handle a surprising number of C++ features, but not all of
4-
them. When `bindgen` can't translate some C++ construct into Rust, it usually
5-
comes down to one of two things:
6-
7-
1. Rust has no equivalent language feature
8-
2. C++ is *hard!*
9-
10-
Notable C++ features that are unsupported or only partially supported, and for
11-
which `bindgen` *should* generate opaque blobs whenever it finds an occurrence
12-
of them in a type it is generating bindings for:
13-
14-
* Template specialization
15-
* Partial template specialization
16-
* Traits templates
17-
* SFINAE
3+
`bindgen` can handle a some C++ features, but not all of them. To set
4+
expectations: `bindgen` will give you the type definitions and FFI declarations
5+
you need to build an API to the C++ library, but using those types in Rust will
6+
be nowhere near as nice as using them in C++. You will have to manually call
7+
constructors, destructors, overloaded operators, etc yourself.
188

199
When passing in header files, the file will automatically be treated as C++ if
2010
it ends in `.hpp`. If it doesn't, adding `-x c++` clang args can be used to
@@ -23,10 +13,54 @@ as well.
2313

2414
You pretty much **must** use [whitelisting](./whitelisting.html) when working
2515
with C++ to avoid pulling in all of the `std::*` types, many of which `bindgen`
26-
cannot handle. Additionally, you may want to mark other types
27-
as [opaque](./opaque.html) that `bindgen` stumbles on.
16+
cannot handle. Additionally, you may want to mark other types as
17+
[opaque](./opaque.html) that `bindgen` stumbles on. It is recommended to mark
18+
all of `std::*` opaque, and to whitelist only precisely the functions and types
19+
you intend to use.
20+
21+
You should read up on the [FAQs](./faq.html) as well.
22+
23+
## Supported Features
24+
25+
* Inheritance
26+
27+
* Methods
28+
29+
* Bindings to constructors and destructors (but they aren't
30+
implicitly/automatically invoked)
31+
32+
* Function and method overloading
33+
34+
* Templates *without* specialization. You should be able to access individual
35+
fields of the class or struct.
36+
37+
## Unsupported Features
38+
39+
When `bindgen` finds a type that is too difficult or impossible to translate
40+
into Rust, it will automatically treat it as an opaque blob of bytes. The
41+
philosophy is that
42+
43+
1. we should always get layout, size, and alignment correct, and
44+
45+
2. just because one type uses specialization, that shouldn't cause `bindgen` to
46+
give up on everything else.
47+
48+
Without further ado, here are C++ features that `bindgen` does not support or
49+
cannot translate into Rust:
50+
51+
* Inline functions and methods: see
52+
["Why isn't `bindgen` generating bindings to inline functions?"](./faq.html#why-isnt-bindgen-generating-bindings-to-inline-functions)
53+
54+
* Template functions, methods of template classes and structs. We don't know
55+
which monomorphizations exist, and can't create new ones because we aren't a
56+
C++ compiler.
57+
58+
* Anything related to template specialization:
59+
* Partial template specialization
60+
* Traits templates
61+
* Specialization Failure Is Not An Error (SFINAE)
2862

29-
Note that using `bindgen` with C++ isn't nearly as plug-and-play as using it
30-
with C is. Improvement is ongoing.
63+
* Cross language inheritance, for example inheriting from a Rust struct in C++.
3164

32-
You might want to read up on the [FAQs](./faq.html) as well.
65+
* Automatically calling copy and/or move constructors or destructors. Supporting
66+
this isn't possible with Rust's move semantics.

0 commit comments

Comments
 (0)