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

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

    Here's a typeid version that uses boost::core::demangle to get the type name at runtime.

    #include <string>
    #include <iostream>
    #include <typeinfo>
    #include <vector>
    using namespace std::literals;
    
    #include <boost/core/demangle.hpp>
    
    template<typename T>
    std::string type_str(){ return boost::core::demangle(typeid(T).name()); }
    
    auto main() -> int{
        auto make_vector = [](auto head, auto ... tail) -> std::vector<decltype(head)>{
            return {head, tail...};
        };
    
        auto i = 1;
        auto f = 1.f;
        auto d = 1.0;
        auto s = "1.0"s;
        auto v = make_vector(1, 2, 3, 4, 5);
    
        std::cout
        << "typeof(i) = " << type_str<decltype(i)>() << '\n'
        << "typeof(f) = " << type_str<decltype(f)>() << '\n'
        << "typeof(d) = " << type_str<decltype(d)>() << '\n'
        << "typeof(s) = " << type_str<decltype(s)>() << '\n'
        << "typeof(v) = " << type_str<decltype(v)>() << '\n'
        << std::endl;
    }
    

    Which prints this on my system:

    typeof(i) = int
    typeof(f) = float
    typeof(d) = double
    typeof(s) = std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
    typeof(v) = std::vector<int, std::allocator<int> >
    
    0 讨论(0)
  • 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 <iomanip>
    #include <iostream>
    
    int
    main()
    {
        using namespace date;
        typedef std::ratio<1, 1> sec;
        std::chrono::duration<double, sec > timePerTick2{0.001};
        auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2;
        std::cout << type_name<decltype(nextTickTime)>() << '\n';
        std::cout << std::setprecision(12) << nextTickTime.time_since_epoch() << '\n';
    }
    

    This output for me:

    std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<double, std::__1::ratio<1, 1000000000> > >
    4.8530542088e+14ns
    
    0 讨论(0)
  • 2021-01-30 08:50

    As Daniel Jour said, read the error message:

    ... _Tp = std::chrono::time_point<
               std::chrono::_V2::system_clock,
               std::chrono::duration<
                 double, std::ratio<1l, 1000000000l> > > ...
    
    0 讨论(0)
  • 2021-01-30 08:53

    A lo-fi trick that doesn't require any prior helper definitions is:

    typename decltype(nextTickTime)::_
    

    The compiler will complain that _ isn't a member of whatever type nextTickTime is.

    0 讨论(0)
  • 2021-01-30 08:57

    Here is a way to force a compile error, which shows the type of tickTime:

    struct {} baD = tickTime;
    
    0 讨论(0)
提交回复
热议问题