Is it possible to print a variable's type in standard C++?

前端 未结 21 1700
南笙
南笙 2020-11-22 01:41

For example:

int a = 12;
cout << typeof(a) << endl;

Expected output:

int
21条回答
  •  無奈伤痛
    2020-11-22 02:18

    You may also use c++filt with option -t (type) to demangle the type name:

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main() {
      auto x = 1;
      string my_type = typeid(x).name();
      system(("echo " + my_type + " | c++filt -t").c_str());
      return 0;
    }
    

    Tested on linux only.

提交回复
热议问题