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

前端 未结 11 1337
伪装坚强ぢ
伪装坚强ぢ 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:48

    This SO answer gives a nice function for printing out the name of a type (actually a couple of implementations).

    Additionally this free, open-source, header-only library gives a nice way to print out the value and type of chrono::durations.

    Putting these two utilities together:

    #include "chrono_io.h"
    #include "type_name.h"
    #include 
    #include 
    
    int
    main()
    {
        using namespace date;
        typedef std::ratio<1, 1> sec;
        std::chrono::duration timePerTick2{0.001};
        auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
        std::cout << type_name() << '\n';
        std::cout << std::setprecision(12) << nextTickTime.time_since_epoch() << '\n';
    }
    

    This output for me:

    std::__1::chrono::time_point > >
    4.8530542088e+14ns
    

提交回复
热议问题