static_cast conversion

来自cppreference.com
< cpp‎ | language

 
 
C++语言
大会的主题
原文:
General topics
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
流量控制
原文:
Flow control
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
条件执行语句
原文:
Conditional execution statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
迭代语句
原文:
Iteration statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
跳转语句
原文:
Jump statements
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
功能
原文:
Functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
函数声明
lambda函数的声明
函数模板
的历史。内嵌说明
异常规范 (已弃用)
noexcept说明 (C++11)
例外
原文:
Exceptions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
命名空间
原文:
Namespaces
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类型
原文:
Types
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
decltype specifier (C++11)
规范
原文:
Specifiers
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
CV符
存储时间说明符
constexpr说明 (C++11)
汽车符 (C++11)
alignas说明 (C++11)
初始化
原文:
Initialization
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
原文:
Literals
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
布尔文字
nullptr (C++11)
用户定义的 (C++11)
表达式
原文:
Expressions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
另一种表示形式
实用工具
原文:
Utilities
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类型
原文:
Types
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
typedef declaration
声明类型别名 (C++11)
属性 (C++11)
施放
原文:
Casts
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
隐式转换
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-风格和功能转换
内存分配
原文:
Memory allocation
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
原文:
Classes
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类特定的功能特性
原文:
Class-specific function properties
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
虚函数
覆盖说明 (C++11)
最后说明 (C++11)
明确的 (C++11)
静态的
特殊的成员函数
原文:
Special member functions
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
模板
原文:
Templates
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
类模板
函数模板
模板特化
参数包 (C++11)
杂项
原文:
Miscellaneous
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
内联汇编
 
之间转换类型使用隐式和用户定义的转换相结合的.
原文:
Converts between types using a combination of implicit and user-defined conversions.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 语法

static_cast < new_type > ( expression )
返回值类型new_type
原文:
Returns a value of type new_type.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 解释

只有下列转换可以static_cast,但这种转换时,将抛弃“常量性”或“波动”
原文:
Only the following conversions can be done with static_cast, except when such conversions would cast away constness or volatility.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
1)
如果一个临时对象类型new_type可以声明和初始化expressionnew_type Temp(expression);,其中可能涉及的隐式转换,调用的构造函数new_type或调用一个用户定义的转换运算符,然后static_cast<type>(expression)计算并返回值该临时对象.
原文:
If a temporary object of type new_type can be declared and initialized with expression, as by new_type Temp(expression);, which may involve implicit conversions, a call to the constructor of new_type or a call to a user-defined conversion operator, then static_cast<type>(expression) computes and returns the value of that temporary object.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
2)
如果new_type是一个指针或引用某个类D的类型expression是其非虚基类的指针或引用Bstatic_cast执行“垂头丧气”。这样的static_cast没有运行时检查,以确保该对象的运行时类型是D,只可这前提是保证安全使用,如果通过其他方式,如在执行静态多态。安全的向下转换可能会与dynamic_cast.
原文:
If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static_cast performs a downcast. Such static_cast makes no runtime checks to ensure that the object's runtime type is actually D, and may only be used safely if this precondition is guaranteed by other means, such as when implementing 静态多态. Safe downcast may be done with dynamic_cast.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
3)
如果new_type是一个右值引用类型,static_cast的价值转换的expression“xvalue”。这种类型的static_cast被用来实现移动语义std::move(C++11 起)
原文:
If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics in std::move. (C++11 起)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
4)
如果new_type的类型void(可能被cv修饰),static_cast丢弃后评估它的价值expression.
原文:
If new_type is the type void (possibly cv-qualified), static_cast discards the value of expression after evaluating it.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
5) 顺序从new_typeexpression的类型存在,不包括左值到右值,数组与指针,函数指针,空指针,空的会员指针,或布尔转换,然后static_cast可以执行逆,隐式转换.
原文:
If an
隐式转换
原文:
implicit conversion
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
</div>
sequence from new_type to the type of expression exists, that does not include lvalue-to-rvalue, array-to-pointer, function-to-pointer, null pointer, null member pointer, or boolean conversion, then static_cast can perform the inverse of that implicit conversion.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
6)
,如果转换expressionnew_type涉及左值,右值,数组与指针,函数指针转换,它可以进行明确的static_cast.
原文:
If conversion of expression to new_type involves lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversion, it can be performed explicitly by static_cast.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
7)
范围的枚举类型可以转换为整数或浮点类型。 (C++11 起)
原文:
Scoped enumeration type can be converted to an integer or floating-point type. (C++11 起)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
8)
整型,浮点型,枚举类型可以转换为任何枚举类型(expression,如果该值转换为枚举的基础类型,是不是一个枚举值的目标,结果是不确定的)
原文:
Integer, floating-point, or enumeration type can be converted to any enumeration type (the result is unspecified if the value of expression, converted to the enumeration's underlying type, is not one of the target enumeration values)
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
9)
一个到一些类D成员的指针,可以上溯造型到一个指针,它的基类B成员。这static_cast成员中实际存在指向的对象的运行时类型检查,以确保.
原文:
A pointer to member of some class D can be upcast to a pointer to member of its base class B. This static_cast makes no checks to ensure the member actually exists in the runtime type of the pointed-to object.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
10)
一个prvalue指针类型void(可能是CV合格的)可以转换成任何类型的指针。作废,并返回到原来的(或更多合格的CV-)型的指针的指针的指针的转换,保留了其原有的价值.
原文:
A prvalue of type pointer to void (possibly cv-qualified) can be converted to pointer to any type. Conversion of any pointer to pointer to void and back to pointer to the original (or more cv-qualified) type preserves its original value.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
与所有的强制转换表达式,其结果是:
原文:
As with all cast expressions, the result is:
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 如果一个左值new_type是一个左值引用类型或一个右值引用为函数类型
    原文:
    an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 值为xValue,如果new_type是一个右值引用的对象类型
    原文:
    an xvalue if new_type is an rvalue reference to object type;
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
  • 一个prvalue否则.
    原文:
    a prvalue otherwise.
    这段文字是通过 Google Translate 自动翻译生成的。
    您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 注释

static_cast也可以通过执行一个函数到指针转换成特定类型,如在std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));用来消除歧义函数重载
原文:
static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 关键字

static_cast

[编辑] 示例

#include <vector>
#include <iostream>
 
struct B {};
struct D : B {};
 
enum class E { ONE, TWO, THREE };
enum EU { ONE, TWO, THREE };
 
int main()
{
    // 1: initializing conversion
    int n = static_cast<int>(3.14); 
    std::cout << "n = " << n << '\n';
    std::vector<int> v = static_cast<std::vector<int>>(10);
    std::cout << "v.size() = " << v.size() << '\n';
 
    // 2: static downcast
    D d;
    B& br = d; // upcast via implicit conversion
    D& another_d = static_cast<D&>(br); // downcast
 
    // 3: lvalue to xvalue
    std::vector<int> v2 = static_cast<std::vector<int>&&>(v);
    std::cout << "after move, v.size() = " << v.size() << '\n';
 
    // 4: discarded-value expression
    static_cast<void>(v2.size());
 
    // 5. inverse of implicit conversion
    // todo
 
    // 6. array-to-pointer followed by upcast
    D a[10];
    B* dp = static_cast<B*>(a);
 
    // 7. scoped enum to int or float
    E e = E::ONE;
    int one = static_cast<int>(e);
 
    // 8. int to enum, enum to another enum
    E e2 = static_cast<E>(one);
    EU eu = static_cast<EU>(e2);
 
    // 9. pointer to member upcast
    // todo
 
    // 10. void* to any type
    void* voidp = &e;
    std::vector<int>* p = static_cast<std::vector<int>*>(voidp);
}

输出:

n = 3
v.size() = 10
after move, v.size() = 0

[编辑] 另请参阅