Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Mutual conversion operators to constructors #95288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Repiteo
Copy link
Contributor

@Repiteo Repiteo commented Aug 8, 2024

While brainstorming ways to make the math struct types play nicely with each other in a constexpr environment, I eventually came across a C++ quirk I wasn't previously aware of: a function with a forward-declared type can itself be forward declared, and only needs to be properly defined if the type itself is resolved. With this in mind, any float-to-int conversions in math structs are able to not only utilize constructors instead of conversion operators, but are able to be safely defined in a constexpr context!

While this isn't a constexpr PR (see #92059), this does open the door for allowing proper interopability between structs defined in entirely separate headers. As for immediate benefits, this slightly simplifies logic by not needing both a constructor and conversion operator, as the former effectively replaces the latter.

@Repiteo Repiteo added this to the 4.4 milestone Aug 8, 2024
@Repiteo Repiteo requested a review from a team as a code owner August 8, 2024 16:52
@Repiteo Repiteo force-pushed the core/math-operator-to-constructor branch from 294e446 to abaae19 Compare September 4, 2024 15:12

Verified

This commit was signed with the committer’s verified signature.
Repiteo Thaddeus Crews
@Repiteo Repiteo force-pushed the core/math-operator-to-constructor branch from abaae19 to 73a13ff Compare November 1, 2024 16:05
Comment on lines +375 to +382
#ifdef RECT2I_H
Rect2::Rect2(const Rect2i &p_rect) :
position(p_rect.position),
size(p_rect.size) {}
Rect2i::Rect2i(const Rect2 &p_rect) :
position(p_rect.position),
size(p_rect.size) {}
#endif // RECT2I_H
Copy link
Member

Choose a reason for hiding this comment

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

I guess this wouldn't play nice with #pragma once.

I don't fully understand how this works but it seems to make things quite dependent of include order?

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.

None yet

2 participants