Convert int64_t to double

前端 未结 4 1727
野趣味
野趣味 2021-02-07 05:31
  int64_t a = 1234;

  double d = (double) a;

Is this the recommended way?

4条回答
  •  灰色年华
    2021-02-07 05:54

    use static_cast as strager answers. I recommend against using the implicit cast (or even a C-style cast in C++ source code) for a few reasons:

    • Implicit casts are a common source of compiler warnings, meaning you may be adding noise to the build (either now, or later when better warning flags are added).
    • The next maintenance programmer behind you will see an implicit cast, and needs to know if it was intentional behavior or a mistake/bug. Having that static_cast makes your intent immediately obvious.
    • static_cast and the other C++-style casts are easy for grep to handle.

提交回复
热议问题