Retrieving the type of auto in C++11 without executing the program

后端 未结 5 1500
悲&欢浪女
悲&欢浪女 2021-01-11 10:10

I have some C++11 code using the auto inferred type that I have to convert to C++98. How would I go about converting the code, substituting in the actual type f

5条回答
  •  时光说笑
    2021-01-11 11:00

    You could use typeid and std::type_info::name();

    #include 
    #include 
    #include 
    
    int
    main()
    {
      using namespace std::literals::complex_literals;
    
      auto x = 3.1415F;
      std::cout << typeid(x).name() << '\n';
      auto z = 1.0 + 1.0i;
      std::cout << typeid(z).name() << '\n';
    }
    
    $ /home/ed/bin_concepts/bin/g++ -std=c++14 typeid.cpp 
    
    $ ./a.out
    f
    St7complexIdE
    

    The names aren't beautiful but you can at least translate them. These names are got from g++. The name is compiler dependent. There is some movement to standardize a pretty_name(). Here is a non-standard way to unmangle the names.

提交回复
热议问题