Skip to content

Spell make_unique for array as the standard does - #574

Merged
nunoplopes merged 3 commits into
Cpp2Rust:masterfrom
lucic71:unique-ptr-array
Oct 31, 2026
Merged

nunoplopes merged 3 commits into
Cpp2Rust:masterfrom
lucic71:unique-ptr-array

Conversation

@lucic71

@lucic71 lucic71 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor
- template <typename T1> std::unique_ptr<T1[]> f1(std::size_t n) {
-  return std::make_unique<T1[]>(n);
+ template <typename T1> auto f1(std::size_t a0) {
+  return std::make_unique<T1>(a0);
}

There is no std::make_unique<T1[]>(n) in the standard, only std::make_unique<T1>(a0). Spell the rules as the standard does.


For that, I added a new marker trait called CppArray that unwraps the element so that the rule for array make_unique can be written as:

unsafe fn f1<T1: CppArray<T2>, T2: Default>(a0: usize) -> Option<Box<[T2]>> {
    Some((0..a0).map(|_| <T2>::default()).collect::<Box<[_]>>())
}

CppArray uses the same infrastructure as CppFrom and CppIterator.


I also had to add a fallback for matching the template arguments. In the new spelling of make_unique, T1 becomes undeducible because it does not appear in the arguments or returns type of the rule. So we deduce T1 from the callee of the return expr.

@nunoplopes
nunoplopes merged commit a908bf0 into Cpp2Rust:master Oct 31, 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