为什么 static_cast<int> 无法将变量转换为 int

0 c++ type-conversion static-cast c++17

我有以下代码,我试图将变量转换为整数,但从倒数第二次打印可以看出,该类型仍被视为“d”-有人知道如何将其更改为 i,因此有它表现得好像它最初被初始化为auto a = 2?

#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

int main()
{
    auto a = 2.3;
    cout << typeid(a).name() << endl;
    a = static_cast<int>(a);
    cout << typeid(a).name() << endl;
    cout << a << endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

eer*_*ika 6

C++ 是一种静态类型语言。变量的类型在运行时不会改变。如果变量的类型是double,那么它永远不会变成其他任何东西。

例如,您可以使用另一个变量:

int b = a;
Run Code Online (Sandbox Code Playgroud)