static_cast<double> 和 double 一样吗?

are static_cast<double> and double the same?

我是 C++ 转换的初学者。 我需要知道 static_cast<double>double 是否可以在任何代码中交换?

在下面的代码中,我可以将static_cast<double>替换为double吗?这要短得多。我会失去任何可读性吗?

a= static_cast<double> ( 3 ) / static_cast<double>( 7 );

其他基本类型如intcharsize_t如何static_cast

根据解释他们一定是一样的。但是有没有特例呢?

刚看完When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?


C++11 标准草案调用 T(number) 函数符号和 (T) number 强制转换符号。鉴于表达式列表是单个表达式,它们是等价的:

§5.2.3/1 A simple-type-specifier (7.1.6.2) or typename-specifier (14.6) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). [...]

(T) number 可以调用 static_cast,在这种情况下具有以下行为:

§5.2.9/4 Otherwise, an expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The expression e is used as a glvalue if and only if the initialization uses it as a glvalue.

您可以节省大量的输入,而只需使用浮动文字(其类型为 double)。

a = 3.0 / 7.0;