Module Linkage

Learn the difference between internal and external linkages with examples.

We'll cover the following

Before C++20

Until C++20, C++ supported two kinds of linkages: internal linkages and external linkages.

  • Internal linkage: Names with internal linkage are not accessible outside the translation unit. The internal linkage includes mainly namespace-scope names that are declared static and members of anonymous namespaces.

  • External linkage: Names with external linkage are accessible outside the translation unit. The external linkage includes names that are not declared as static. The external linkage allows class types, and their members, variables, and templates.

After C++20

Modules introduce module linkage:

  • Module linkage: Names with module linkage are only accessible inside the module. Names have module linkage if they don’t have internal linkage and they are not exported.

A small variation of the previous module declaration mathModuleTemplate.ixx makes my point. Imagine that I want to return to the user of my function template sum not only the result of the addition but also the return type the compiler deduces.

Get hands-on with 1200+ tech skills courses.