本文介绍了将int64_t转换为double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int64_t a = 1234;

  double d = (double) a;

这是推荐的方法吗?

推荐答案

使用 static_cast 我建议使用隐式转换(或甚至C ++源代码中的C风格的转换)来反对 ,原因如下:

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:


  • 隐式强制转换是编译器警告的常见来源,这意味着您可能会向构建中添加噪点(现在或以后添加更好的警告标记时)。

  • 在你后面的维护程序员将看到一个隐式转换,并需要知道它是故意的行为还是一个错误/错误。让 static_cast 立即显示您的意图

  • static_cast 和其他C ++风格的转型可以很容易地处理 grep

  • 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.

这篇关于将int64_t转换为double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:02