Skip to content

Hoist static locals to module scope - #491

Open
lucic71 wants to merge 2 commits into
Cpp2Rust:masterfrom
lucic71:14-hoist-static-locals
Open

lucic71 wants to merge 2 commits into
Cpp2Rust:masterfrom
lucic71:14-hoist-static-locals

Conversation

@lucic71

@lucic71 lucic71 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@nunoplopes

Copy link
Copy Markdown
Contributor

why? It's better to keep them inside the function to be closer to the original code.

@lucic71

lucic71 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

why? It's better to keep them inside the function to be closer to the original code.

int from_local_class() {
  static int x = 3;
  struct S {
    int get() const { return ++x; }
  };
  return S{}.get() + S{}.get();
}

S is already hoisted as module scope. Because of that it does not have access to the static variable. Hoist x at module scope so that S can have access to it.

This branch has not been deployed

No deployments
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.

2 participants