Using 'auto' type deduction - how to find out what type the compiler deduced?

前端 未结 11 1262
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 07:54

How can I find out what type the compiler deduced when using the auto keyword?

Example 1: Simpler

auto tickTime = 0.001;

W

11条回答
  •  不要未来只要你来
    2021-01-30 08:38

    typeid can be used to get the type of variable most of the time. It is compiler dependent and I've seen it give strange results. g++ has RTTI on by default, not sure on the Windows side.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef std::ratio<1, 1> sec;
    int main()
    {
        auto tickTime = .001;
        std::chrono::duration timePerTick2{0.001};
        auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
        std::cout << typeid(tickTime).name() << std::endl;
        std::cout << typeid(nextTickTime).name() << std::endl;
    
        return 0;
    }
    
    ./a.out | c++filt
    
    double
    std::__1::chrono::time_point > >
    

提交回复
热议问题