Skip to content

C++ generator emits non-const static data members with in-class initializers (static int count = 0;), which doesn't compile #80

Closed

Description

@matt-edmondson

What's wrong

For a static field that isn't constant, CppGenerator.SpellStorage (Coder/Languages/CppGenerator.cs:~790-798) returns "static ". GenerateFieldDeclaration (~565-585) then always adds an initializer: either = value or {}.

Failure scenario

Input: ClassDeclaration("Cfg") with FieldDeclaration("count", "int") { IsStatic = true, InitialValue = Literal.Number(0) }.

It emits static int count = 0; inside the class. g++ -std=c++20 rejects this with "ISO C++ forbids in-class initialization of non-const static member 'Cfg::count'". Dropping the initializer doesn't help: the generator then emits static int count{};, which fails with the same error.

Every class with a mutable static field therefore produces uncompilable C++.

Suggested fix

Inside a type, spell a non-const static field as inline static (C++17), which permits an in-class initializer:

inline static int count = 0;

Add a test that compiles, or at least snapshots, a class with a mutable static field.

Activity

  1. added
    bug
    on Sep 26, 2026
  2. matt-edmondson commented on Sep 26, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions