简体   繁体   English

在C ++中,static_cast之间有什么区别 <double> (a)和双(a)?

[英]In C++, what are the differences between static_cast<double>(a) and double(a)?

What are the differences between 有什么区别

int a;
// a gets some value
double pi = static_cast<double>(a)/3;

and

int a;
// a gets some value
double pi = double(a)/3;

Have you ever seen the latter? 你见过后者吗? It seems to me I saw it in some snippet written by Stroustrup but I can't find the reference. 在我看来,我在Stroustrup写的一些片段中看到了它,但我找不到参考。

Someone may have thought they were constructing rather than casting. 有人可能认为他们正在建造而不是铸造。 Consider: 考虑:

some_fun(std::string("Hello"));

Many people think they're calling a constructor there when in fact they're doing a C-style cast. 许多人认为他们在那里调用构造函数,而实际上他们正在进行C风格的演员。 It just so happens that casting will look at constructors of the target type among the long list of other things it looks at and so here it eventually ends up invoking the constructor. 事实上,在它看到的其他东西的长列表中,cast会查看目标类型的构造函数,因此它最终会调用构造函数。

Functional notation casts have all the same weaknesses of the other kind of C cast: 功能表示法强制转换与其他类型的C强制转换具有相同的弱点:

  • Can inadvertently cast away constness 可能会无意中抛弃constness
  • Can silently turn into a reinterpret cast 可以默默地变成重新诠释
  • Are hard to differentiate with grepping tools. 很难用grepping工具区分。

Besides all that though, you're performing exactly the same operation in both cases. 除此之外,在这两种情况下,您执行的操作完全相同。

The latter is referred to as the functional notation of explicit casting where you explicitly say a should be treated as a double . 后者被称为显式转换功能表示法 ,其中您明确地说a应该被视为double You can pretty much cast anything to any type using this technique. 你可以使用这种技术将任何类型的任何东西抛出来。

The former is the preferred way to cast a type in C++. 前者是在C ++中强制转换类型的首选方法。 It does basic checking to see that the type you are casting to makes sense (child class pointer to a base class pointer, etc.). 它执行基本检查以查看您要转换的类型是否有意义(指向基类指针的子类指针等)。 In addition, like in the example you show, you can perform implicit conversions. 此外,与您显示的示例一样,您可以执行隐式转换。 Technically the static_cast in your example is explicit, but the result of the operation (the assignment) is implicit. 从技术上讲,示例中的static_cast是显式的,但操作(赋值)的结果是隐式的。

There is no difference in terms of generated assembly code between static_cast<double> (a) and (double) a . static_cast<double> (a)(double) a之间生成的汇编代码没有区别。 The key advantage of cast notation, (type_id) cast_expression , is that it is more flexible. (type_id) cast_expression符号(type_id) cast_expression的主要优点是它更灵活。 In one situation it might be the equivalent of a const_cast , in another, a static_cast , in yet another, a dynamic_cast , in yet another, a combination of const_cast and static_cast (or dynamic_cast ). 在一种情况下,它可能相当于const_cast ,在另一种情况下, static_cast ,在另一种情况下, dynamic_cast ,在另一种情况下, const_caststatic_cast (或dynamic_cast )的组合。

This strength is also a weakness. 这种力量也是一个弱点。 Cast notation means different things in different places. 演员表示法在不同的地方意味着不同的东西。 Another disadvantage is that it is very easy to find xxx_cast<type_id> (cast_expression) . 另一个缺点是很容易找到xxx_cast<type_id> (cast_expression) Just search for _cast . 只是搜索_cast It is very hard to find expressions that use cast notation. 很难找到使用强制表示法的表达式。

using static_cast is a safe C++-style way, but (double) - unsafe old C-style way. 使用static_cast是一种安全的C ++风格方式,但是(双重) - 不安全的旧C风格方式。

see here: Type Casting 看到这里: 类型铸造

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM