简体   繁体   English

我是否真的每次都想在原始类型之间进行转换时使用static_cast?

[英]Should I really use static_cast every single time I want to convert between primitive types?

What makes this 是什么让这个

long l = 1;
char c = static_cast<char>(l);

float f = 1.0f;
int i = static_cast<int>(f);

better than this 比这更好

long l = 1;
char c = (char)l;

float f = 1.0f;
int i = (int)f;

when casting one primitive data type to another? 何时将一个原始数据类型转换为另一个?

I've got much of legacy code that uses the second style for type casting in similar situations, so this is also a question about should I or may I not perform full-scale revision of that code. 我有很多遗留代码在类似的情况下使用第二种样式进行类型转换,所以这也是一个问题,我是否可以不执行该代码的全面修订。

Future-proofing. 面向未来。

Let's say in the future I do this: 让我们说将来我这样做:

float blah = 1.0f;
float* f = &blah;

Now, int i = static_cast<int>(f); 现在, int i = static_cast<int>(f); stops compiling, but int i = (int)f; 停止编译,但int i = (int)f; does a reinterpret_cast . 做一个reinterpret_cast

static_cast<int> is this is exactly what I want you to do . static_cast<int> 这正是我想要你做的 (int) is do whatever you can to get me an int . (int) 尽你所能让我得到一个int With the latter, the compiler will go out of its way to get you an int value, and that's rarely (never?) desirable. 对于后者,编译器将不遗余力地为您获取一个int值,而这很少(从不?)是可取的。

The every single time is a bit of an indication. 每一次都是一个迹象。

You shouldn't be converting between primitive types so often that typing a few extra characters each time is an overly onerous task. 你不应该经常在原始类型之间进行转换,因此每次输入一些额外的字符是一项过于繁重的任务。 It's kind of like complaining that you have to wear a helmet every time you do some dangerous activity. 这有点像抱怨每次做一些危险的活动时都要戴头盔。 If you're finding wearing the helmet too annoying, the problem is likely that you're engaging in dangerous activity too often rather than the helmet requirement. 如果你发现戴头盔太烦人了,问题很可能是你经常从事危险活动而不是头盔要求。

As for addressing legacy code, you can check out this question for some answers (including my own). 至于解决遗留代码,您可以查看此问题以获得一些答案(包括我自己的答案)。

Yes, you should. 是的你应该。

Casts are a major source of bugs. 演员阵容是错误的主要来源。 They are ways around the type-system, which is one of the best bug catchers available to programmers. 它们是类型系统的方法,它是程序员可以使用的最好的错误捕获程序之一。

A static_cast is much more visible and much more specific than an old c-style cast. static_cast比旧的c风格的演员更加明显和更具体 You want them to stand out. 你希望他们脱颖而出。 You want them to be obvious when you're debugging. 在调试时,您希望它们显而易见。 You want the next programmer to come along to understand why you're doing it. 你希望下一个程序员能够理解你为什么要这样做。

The fact that it's harder to type static_cast<blah>(foo) is also a benefit, as it'll encourage you to cast only when absolutely necessary. 输入static_cast<blah>(foo)更难的事实也是一个好处,因为它会鼓励你只在绝对必要的情况下施放。

A constructor cast is an alternative. 构造函数强制转换是另一种选择。 Assuming there is a conversion constructor defined. 假设定义了转换构造函数。 For example: 例如:

int i(0);
float f(3.14F);
i = int(f);

However you should definitely prefer static_cast and other C++ casts over a C-style cast as those basically say "try every cast until something works" which will eventually hit the equivalent of a reinterpret_cast and probably give you incorrect behavior. 但是你肯定更喜欢static_cast广播和其他C ++演员而不是C风格的演员static_cast ,因为那些基本上会说“尝试每次演员直到有效”,这最终会达到相当于reinterpret_cast并且可能会给你不正确的行为。

The issue with C-style casts is that it -may let you do conversions you didn't intend. C风格演员表的问题在于它可以让你做你不想要的转换。

It is something you should do in the future but I wouldn't go back and change it unless you do find an issue. 这是你将来应该做的事情,但除非你找到问题,否则我不会回去改变它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用它还是static_cast <void*> 然后是static_cast <myType*> 避免重新解释? - should I use it or static_cast<void*> then static_cast<myType*> to avoid reinterpret_cast? 我应该在赋值和返回语句中使用static_cast吗?为什么? - Should I use static_cast in assignments and return statements and why? 原始引用之间的static_cast - static_cast between primitive references 如果我想将char提升为int,应该使用static_cast <int> (字符变量)或+(字符变量),为什么? - If I want to promote a char to an int, should I use static_cast<int>(char variable) or +(char variable) and why? 将 void* 转换为任何内容时,我应该使用 static_cast 还是 reinterpret_cast - Should I use static_cast or reinterpret_cast when casting a void* to whatever “无关类型”之间的static_cast - static_cast between 'unrelated types' 为什么我应该在C ++中使用&#39;static_cast&#39;进行数值转换? - Why should I use 'static_cast' for numeric casts in C++? 我应该使用static_cast还是INT64_C来便携地分配64位常量? - Should I use static_cast or INT64_C to assign 64-bit constant portably? 为什么我可以对static * cast使用void *但不能对char *使用 - Why can I use static_cast With void* but not With char* 我可以使用static_cast进行向下投射吗? - Can I use static_cast to down casting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM