explicit type conversion
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
之间转换使用相结合的显式和隐式转换的类型.
原文:
Converts between types using a combination of explicit and implicit conversions.
[编辑] 语法
( new_type ) expression
|
(1) | ||||||||
new_type ( expression )
|
(2) | ||||||||
返回值类型
new_type
[编辑] 解释
1)
当C风格的转换表达时,编译器会尝试以下CAST表达式,顺序如下:
原文:
When the C-style cast expression is encountered, the compiler attempts the following cast expressions, in this order:
a) const_cast<new_type>(expression)
b)
static_cast<new_type>(expression),扩展名:到派生类指针或引用另外允许将其转换为明确的基类的指针或引用(反之亦然),即使无法访问基类(也就是,这个演员忽略了私有继承规范) 。同样适用于铸造成员无二非虚基类成员的指针的指针
原文:
static_cast<new_type>(expression), with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambigous non-virtual base
c)
static_cast(扩展名),其次const_cast
原文:
static_cast (with extensions) followed by const_cast
d) reinterpret_cast<new_type>(expression)
e)
reinterpret_cast其次是const_cast
原文:
reinterpret_cast followed by const_cast
@ @的第一选择满足要求的各自的类型转换操作符被选择时,即使它不能被编译(参见实施例).
原文:
@@ The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled (see example).
@ @此外,C-风格的转换符号允许投,之间,以及不完整的类类型的指针。如果expression和new_type是不完整的类类型的指针,它指定是否被选中的static_cast和reinterpret_cast的.
原文:
@@ In addition, C-style cast notation is allowed to cast from, to, and between pointers to incomplete class type. If both expression and new_type are pointers to incomplete class types, it's unspecified whether static_cast or reinterpret_cast gets selected.
2)
功能转换由一个简单的类型说明符或typedef说明符(换句话说,单个单词的类型名称:unsigned int(expression)或int*(expression)无效),然后由一个单一的括号中的表达式。这个转换是完全等同于相应的C风格的转换表达.
原文:
The functional cast consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses. This cast is exactly equivalent to the corresponding C-style cast expression.
与所有的强制转换表达式,其结果是:
原文:
As with all cast expressions, the result is:
- 如果一个左值new_type是一个左值引用类型或一个右值引用为函数类型原文:an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
- 值为xValue,如果new_type是一个右值引用的对象类型原文:an xvalue if new_type is an rvalue reference to object type;
- 一个prvalue否则.
[编辑] 示例
double f = 3.14; unsigned int n1 = (unsigned int)f; // C-style cast unsigned int n2 = unsigned(f); // functional cast class C1; class C2; C2* foo(C1* p) { return (C2*)p; // casts incomplete type to incomplete type } // In this example, C-style cast is interpreted as static_cast // even though it would work as reinterpret_cast struct A {}; struct I1 : A {}; struct I2 : A {}; struct D : I1, I2 {}; int main() { D* d = nullptr; A* a = (A*)d; // compile-time error A* a = reinterpret_cast<A*>(d); // this compiles }