Skip to content

Use default literals instead of Default::default() in const initialization#66

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:const-default
May 10, 2026
Merged

Use default literals instead of Default::default() in const initialization#66
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:const-default

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 10, 2026

Default::default() is not a const function, hence it cannot be used to initialize global objects such as pub static mut static_outer: Outer = Outer::default().

To overcome this limitation, emit the default literal for the object, for example:

pub static mut static_outer: Outer = Outer {
    p1: std::ptr::null_mut(),
    p2: std::ptr::null(),
    arr: [std::ptr::null_mut(); 3],
    cp: std::ptr::null(),
    pp: std::ptr::null_mut(),
    inner: Inner {
        v: 0_i32,
        name: std::ptr::null(),
    },
    x: 0_i32,
    fn_: None,
};

@nunoplopes nunoplopes merged commit a8625eb into Cpp2Rust:master May 10, 2026
9 checks passed
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