为什么编译器允许用户更改“自动”变量的类型?

Spe*_*702 2 c++ g++ auto

我希望auto关键字从初始化器中推断出变量的类型一次,并在整个代码中保持该类型。令我惊讶的是,我的编译器 ( g++ 9.3.0) 允许我更改它的类型并且它仍然有效。当我第一次使用变量作为 int 然后作为 float 时,这对我来说是可以接受的。但是,当我使用auto关键字声明一个字符串,然后为该变量分配一个浮点值时,编译器不会抛出错误,但也不会在浮点赋值后打印该值。有人可以解释为什么它允许首先将浮点值分配给字符串变量吗?编译器每次都只接受新的赋值吗?或者它是否抛出某种我无法捕获的异常?下面的代码 -

#include <iostream>

int main()
{
 auto x = '2';
 
 std::cout << x << std::endl;
 
 x = 3.0; // Why is this allowed?
 
 std::cout << x << std::endl; // This won't print
 
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

P K*_*mer 6

为了向您展示发生了什么,我通过一些编译时类型检查扩展了该示例:

#include <type_traits>
#include <iostream>

int main()
{
    auto x = '2';

    // decltype(x) is the type for variable x
    // compare it at compile time with char type
    static_assert(std::is_same_v<decltype(x), char>);

    std::cout << x << std::endl;

    x = 3.0; // Why is this allowed? because it does a narrowing conversion from double to char
    // my compiler even gives this warning :
    // main.cpp(11,6): warning C4244: '=': conversion from 'double' to 'char', possible loss of data
    
    // type of x is still the same and it is still a char
    static_assert(std::is_same_v<decltype(x), char>);

    std::cout << static_cast<int>(x) << std::endl; // the cast is here to be able to print the actual value of the char

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