Skip to content

internal linkage for constexpr variables for module use#2428

Merged
stephenberry merged 1 commit intomainfrom
internal-linkage-constexpr-variables
Apr 2, 2026
Merged

internal linkage for constexpr variables for module use#2428
stephenberry merged 1 commit intomainfrom
internal-linkage-constexpr-variables

Conversation

@stephenberry
Copy link
Copy Markdown
Owner

@stephenberry stephenberry commented Apr 2, 2026

Added inline to namespace-scope constexpr variables (giving them external linkage), and removed static from function templates.


constexpr functions are implicitly inline, but constexpr variables are not — they're implicitly const, and const at namespace scope has internal linkage in C++.

So:

// Internal linkage (each TU gets its own copy)
constexpr int x = 42;

// External linkage (one definition shared across TUs)
inline constexpr int x = 42;

This distinction doesn't matter in traditional header-based code — each TU gets its own identical copy and the linker is fine with it. But with C++ modules, GCC 15 enforces [basic.link]/[module.reach]: a module interface unit must not expose entities with internal linkage (TU-local entities) in any reachable declaration. Since templates and inline functions in the module interface reference these constants, the internal-linkage variables "leak" through, and GCC rightfully rejects it.

@stephenberry stephenberry linked an issue Apr 2, 2026 that may be closed by this pull request
@stephenberry stephenberry merged commit 06df57d into main Apr 2, 2026
44 of 49 checks passed
@stephenberry stephenberry deleted the internal-linkage-constexpr-variables branch April 2, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Errror with C++ module

1 participant