0
hours = static_cast<double>(minutes)/ 60

and

double minutes
hours = int (minutes)/ 60
  1. is constrained casts? correct me if i'm wrong

whats the second one called? and whats the difference if they both will produce the same result?

if then which is better to use?

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • Well the second is *undefined behavior* since you don't initialize or assign to `minutes`. Don't know about the first. And there's no way of telling if both will produce the same result with the information you give us, especially since the conversions are to different types. Please create a proper [mre] to show us. – Some programmer dude Apr 17 '23 at 19:30
  • 2
    And if you haven't done it yet, then please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [how to write the "perfect" question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/), especially its [checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 17 '23 at 19:31
  • Well, at least one is to `double`, and the other is to `int`, here's the difference. If you use the same type, there's no difference in this case. In general, there's no (or negligible) difference when the target type isn't a pointer nor a reference. If they are, the second form allows some sloppy casts, and the first doesn't, so the second form shouldn't generally be used to cast to pointers and references. It's fine otherwise, though some people take a radical "second form bad" stance. – HolyBlackCat Apr 17 '23 at 19:34
  • Related: [What exactly is or was the purpose of C++ function-style casts?](https://stackoverflow.com/q/4474933/509868) – anatolyg Apr 17 '23 at 19:40

1 Answers1

1

Syntax:

int (minutes) is called "function cast" or "functional-style cast", because it looks like a function call. It does the same as C-style cast (int)minutes — truncates double to int. The difference between this and static_cast is only in syntax: the latter is more verbose, so easier to search for in code.

static_cast is different from C-style cast in type-safety — it fails to do suspicious conversions (e.g. integer to pointer). Other than that, it's identical.


Semantics:

hours = static_cast<double>(minutes)/ 60

This does floating-point division, generating a floating-point result. If hours has floating-point type (e.g. double), it will retain fractional part (e.g. 10 minutes is 0.16666666666666666 hours).

If hours has integer type, the compiler will generate type conversion from double to int (and probably a warning) and truncate the number of hours. This is bad code, because it hides some of your calculation logic behind obscure language rules. If you want to convert from double to int, do it explicitly using static_cast or C-style cast.

double minutes;
hours = int (minutes)/ 60

This truncates minutes to integer and calculates an integer number of hours from that. A bit surprising that minutes can hold non-integer values but hours cannot, so you might want to explain your calculation logic in a comment.

anatolyg
  • 26,506
  • 9
  • 60
  • 134