Closed. This question is opinion-based。它当前不接受答案。












想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。

去年关闭。



Improve this question




考虑:
int total = 14; // something for which double does not make sense
int nPeople = 5;
double perCapita = static_cast<double>(total) / nPeople; // should this cast be avoided?

在这种情况下应该避免铸造吗?编写1.0 * total / nPeople还是其他更好的方法?

最佳答案



乘以1.0不能使意图清楚。此处的显式转换更加清楚,并且是执行此操作的事实上的方式。如果我们有某种可以显式调用的模板函数(一个不称为std::div,因为这是另一种划分),那就太好了-但是las,直到那时,我们才知道,只是static_cast<double>了!

关于c++ - 应该避免这种从int到double的静态转换吗? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58004678/

10-10 00:42