Skip to content

From C++ static_cast to ctor-like conversion #1227

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

Merged
merged 1 commit into from
Aug 19, 2023
Merged

Conversation

hollasch
Copy link
Collaborator

Instead of static_cast<double>(x) switch to double(x) for easier readability for non-C++ readers and simple for C++ readers alike. In the end, these are the same thing.

Resolves #1222

Instead of static_cast<double>(x) switch to double(x) for easier
readability for non-C++ readers and simple for C++ readers alike. In the
end, these are the same thing.

Resolves #1222
@hollasch hollasch added this to the v4.0.0 milestone Aug 19, 2023
@hollasch hollasch self-assigned this Aug 19, 2023
Copy link
Contributor

@armansito armansito left a comment

Choose a reason for hiding this comment

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

Thank you for the clean up, I think this is a good improvement overall.

P.S.:
Something occurred to me while looking through the diff (though I don't want this to block this PR): the code examples currently use auto frequently. Sometimes this is good for brevity (for example with gnarly template expansions) but there are a few spots where I think auto obscures the code a little bit.

For instance, from src/TheNextWeek/perlin.h:

auto u = p.x() - floor(p.x());
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());
auto i = int(floor(p.x()));
auto j = int(floor(p.y()));
auto k = int(floor(p.z()));

The types should generally be clear if you read it carefully but I think it would be clearer with explicit type annotations, especially given the intentional type conversions involved:

double u = p.x() - floor(p.x());
double v = p.y() - floor(p.y());
double w = p.z() - floor(p.z());
int i = int(floor(p.x()));
int j = int(floor(p.y()));
int k = int(floor(p.z()));

Similarly, in src/TheRestOfYourLife/camera.h the assignment to viewport_height relies on implicit type deduction, which should be a double but this may not be immediately obvious upon reading. I think it would be clearer to express that line as:

double viewport_height = 2 * h * focus_dist;
double viewport_width = viewport_height * (double(image_width)/image_height);

Just food for thought.

@hollasch hollasch merged commit 1b8b4e4 into dev Aug 19, 2023
@hollasch hollasch deleted the simple-type-conversion branch August 19, 2023 21:51
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