首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

static_cast conversion

使用隐式转换和用户定义转换的组合在类型之间进行转换。

句法

static_cast < new_type > ( expression )

?

?

返回类型的值。new_type...

解释

只有以下转换可以使用static_cast,除非这种转换会被抛弃。恒恒波动率...

1%29如果有隐式转换序列从表达到新[医]类型,或者如果重载解析为直接初始化对象或新类型引用的[医]从表达式中键入至少会找到一个可行的函数。static_cast<new_type>(expression)返回虚变量。Temp初始化为new_type Temp(expression);,这可能涉及隐式转换,呼叫构造器新的[医]输入或调用用户定义的转换运算符.非参考书[医]类型,则为静态的结果对象。[医]自C++17%29以来,转换prvalue表达式是%27s直接初始化的%28。

2%29如新[医]类型是指向某个类的指针或引用。D表达式的类型是指向其非虚拟基的指针或引用。B,,,static_cast执行下浇...这种下流是不正常的,如果...B中的虚拟基%28或虚拟基%29的基值是模棱两可的、不可访问的或虚拟基%28。D...static_cast不进行运行时检查,以确保对象%27s运行时类型实际上是D,并且只有在此前提条件通过其他方法得到保证时才能安全地使用,例如在实现时。静态多态性.安全的下播可在dynamic_cast...

3) If new_type is an rvalue reference type, static_cast converts the value of glvalue, class prvalue, or array prvalue (until C++17)any lvalue (since C++17) expression to xvalue referring to the same object as the expression, or to its base sub-object (depending on new_type). If the target type is an inaccessible or ambiguous base of the type of the expression, the program is ill-formed. If the expression is a bit field lvalue, it is first converted to prvalue of the underlying type. This type of static_cast is used to implement move semantics in std::move.

(since C++11)

4%29如新[医]类型是void%28可能是cv-合格%29,static_cast在计算后丢弃表达式的值。

5%29标准转换新序列[医]类型的表达式存在,它不包括从lvalue到rvalue,数组到指针,函数到指针,空指针,空成员指针,函数指针,%28自C++17%29或布尔值转换。static_cast可以执行该隐式转换的逆转换。

6%29如果表达式转换为新的[医]类型涉及lvalue到rvalue、数组到指针或函数到指针的转换,它可以通过以下方式显式执行:static_cast...

7%29作用域枚举类型可以转换为整数或浮点类型。当目标类型是CVbool,结果是false如果原始值为零,则true所有其他价值。对于其余的整型,如果枚举可以用目标类型表示,则结果是枚举的值,而不是指定的值。%28自C++11%29

8%29整数、浮点数或枚举类型可转换为任何完整类型。枚举类型如果转换为枚举%27s基础类型的表达式值超出范围%28(如果基础类型是固定的,则范围是类型的范围),则结果为未指定的%28,直到C++17%29自C++17%29。如果基础类型不固定,则范围是最小位字段的所有可能值,该值足够容纳目标枚举数%29的所有枚举数。

9%29指向某类成员的指针D可以向上转换到指向其明确、可访问基类的成员的指针。B.这个static_cast不进行任何检查,以确保成员在指向对象的运行时类型中实际存在。

10%29类型指针的prvaluevoid%28可能是cv-限定%29可以转换为指向任何类型的指针。如果原始指针的值满足目标类型的对齐要求,则结果指针值不变,否则未指定。将任何指针转换为无效指针并返回到指向原始%28或更多cv的指针--限定%29类型保留其原始值。

与所有强制转换表达式一样,结果是:

  • 如果是新的[医]类型是lvalue引用类型或函数类型的rvalue引用;
  • 如果是新的xvalue[医]类型是对对象类型的rvalue引用;
  • 要价不一样。

注记

static_cast还可以通过执行函数到指针转换为特定类型来消除函数重载的歧义,如std::transform(s.begin(), s.end(), s.begin(), static_cast<int(*)(int)>(std::toupper));

关键词

static_cast...

二次

代码语言:javascript
复制
#include <vector>
#include <iostream>
 
struct B {
    int m = 0;
    void hello() const {
        std::cout << "Hello world, this is B!\n";
    }
};
struct D : B {
    void hello() const {
        std::cout << "Hello world, this is D!\n";
    }
};
 
enum class E { ONE = 1, TWO, THREE };
enum EU { ONE = 1, 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
    br.hello();
    D& another_d = static_cast<D&>(br); // downcast
    another_d.hello();
 
    // 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
    void* nv = &n;
    int* ni = static_cast<int*>(nv);
    std::cout << "*ni = " << *ni << '\n';
 
    // 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);
    std::cout << one << '\n';
 
    // 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
    int D::*pm = &D::m;
    std::cout << br.*static_cast<int B::*>(pm) << '\n';
 
    // 10. void* to any type
    void* voidp = &e;
    std::vector<int>* p = static_cast<std::vector<int>*>(voidp);
}

二次

产出:

二次

代码语言:javascript
复制
n = 3
v.size() = 10
Hello world, this is B!
Hello world, this is D!
after move, v.size() = 0
*ni = 3
1
0

二次

另见

  • 康斯特[医]铸造
  • 动态[医]铸造
  • 重释[医]铸造
  • 显式铸造
  • 隐式转换
代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com