Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace boost::openmethod::aliases;
struct Tiger : Carnivore {};

BOOST_OPENMETHOD_CLASSES(Tiger, Carnivore);
BOOST_OPENMETHOD_CLASSES(Animal, Herbivore, Cow, Carnivore, Wolf);

BOOST_OPENMETHOD_OVERRIDE(
meet, (virtual_ptr<Herbivore> a, virtual_ptr<Carnivore> b), std::string) {
Expand Down
15 changes: 14 additions & 1 deletion doc/modules/ROOT/pages/shared_libraries.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ include::{shared}/implicit_linking/extensions.cpp[tag=content]
----

The program extends it. It adds a class the library has never heard of, two
overriders that specialise `meet`, and imports the state through the header:
overriders that specialise `meet`, and imports the state through the header. It
also registers the library's classes a second time - see below:

[source,c++]
----
Expand All @@ -131,6 +132,18 @@ the library's `Animal, Animal` overrider, so control crosses the module boundary
in the middle of a single dispatch, and `cow meets wolf` prints
`do not greet, run`.

The program registers `Animal`, `Herbivore`, `Cow`, `Carnivore` and `Wolf`,
although the library has registered them already. That is the rule
cpp:use_classes[] states: a class must be registered in as many translation
units as it has type ids. On ELF and Mach-O it has one - the dynamic linker
merges the `type_info` objects, and the second registration is redundant but
harmless. Windows keeps one `type_info` per module, so the program's `Wolf` and
the library's `Wolf` are two different type ids, and the program creates its
`Wolf` with `new`. Without the second registration the program's id is unknown
to the registry, and the first dispatch on it fails with `unknown class Wolf`.
Registering in both modules is what makes the example portable, and costs
nothing where it is not needed.

## Dynamic Linking

By "dynamic linking", we mean a program loading a shared library after it has
Expand Down
Loading