Skip to content

Fix LocalVector<T>::clear when T has no default constructor - #123581

Merged
Repiteo merged 1 commit into
godotengine:masterfrom
HolonProduction:lvec-constructor-fail
Sep 18, 2026
Merged

Repiteo merged 1 commit into
godotengine:masterfrom
HolonProduction:lvec-constructor-fail

Conversation

@HolonProduction

Copy link
Copy Markdown
Member

What problem(s) does this PR solve?

This code did previously not compile:

struct Test {
	int i;
	Test() = delete;
	explicit Test(int p_i) : i(p_i) {}
};

LocalVector<Test> t;
t.push_back(Test(1));
t.clear();

because LocalVector::clear did previously use resize internally, it required T to have a default constructor.

@HolonProduction HolonProduction added this to the 4.x milestone Sep 17, 2026
@HolonProduction
HolonProduction requested a review from a team as a code owner September 17, 2026 17:48

@Ivorforce Ivorforce left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange problem to have but I agree this makes sense.

Comment thread core/templates/local_vector.h
Comment thread core/templates/local_vector.h
@lawnjelly

lawnjelly commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

First I was like "huh, won't it also fail with resize(0)"?

And it will, but I guess this is for the pattern of pushing back initialized values, and clear shouldn't require a default constructor logically.

The problem with resize() is that the compiler sees it touches the default constructor.

So I also agree with the PR (even if it's a little strange having to do this).

@HolonProduction

Copy link
Copy Markdown
Member Author

huh, won't it also fail with resize(0)

Yeah if some operations fail that's fine, the issue is primarily that the local vector destructor uses clear. So effectively it makes LocalVector entirely useless if you have no default constructor (or a private one). In practice this just isn't common because most classes need a default constructor for serialization.

@Repiteo Repiteo modified the milestones: 4.x, 4.8 Sep 17, 2026
@Repiteo
Repiteo merged commit 2d60328 into godotengine:master Sep 18, 2026
20 checks passed
@Repiteo

Repiteo commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Thanks!

@HolonProduction
HolonProduction deleted the lvec-constructor-fail branch September 22, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants