Don't use static cast for arithmetic conversions (cpp-core-guidelines)

霸气de小男生 提交于 2021-01-28 04:22:53

问题


msvc's code analyzer for the cpp core guidelines tells me

Warning C26472 Don't use a static_cast for arithmetic conversions. Use brace initialization, gsl::narrow_cast or gsl::narrow (type.1).

for this snippet

static_cast<IntType>(static_cast<unsigned long long>(hexValue(digit)) << (digitIdx * 4));

Why shouldn't I use static_cast here?

Also, with brace init this would look like this

IntType{unsigned long long{hexValue(digit)} << (digitIdx * 4)};

which doesn't look any better imo. This looks more like a function style cast than anything else.

I cannot use gsl and I think gsl::narrow is a wrapper around static_cast itself, so is this purely a readability issue here?


回答1:


so is this purely a readability issue here?

Nope. Brace initialization prohibits narrowing conversions, and will cause a diagnostic. The guideline's purpose is to help protect code from unintended narrowing. A static_cast will allow a narrowing conversion through silently.

You seem to be dealing in integers, so short of using an extended integer type that is larger than unsigned long long, you'll probably not hit any narrowing.

But the guideline is there for the general case, and it's better to write code consistently, even when there is no actual risk.




回答2:


Here is a link to Microsoft documentation:

[https://docs.microsoft.com/en-us/cpp/code-quality/c26472?view=vs-2019][1]

  • gsl::narrow ensures lossless conversion and causes run-time crash if it is not possible.
  • gsl::narrow_cast clearly states that conversion can lose data and it is acceptable.

static_cast doesn't perform these checks, so it is safer to explicitly state your intentions.



来源:https://stackoverflow.com/questions/62410916/dont-use-static-cast-for-arithmetic-conversions-cpp-core-guidelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!