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

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

For example:

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

Expected output:

int
21条回答
  •  忘了有多久
    2020-11-22 02:16

    Try:

    #include 
    
    // …
    std::cout << typeid(a).name() << '\n';
    

    You might have to activate RTTI in your compiler options for this to work. Additionally, the output of this depends on the compiler. It might be a raw type name or a name mangling symbol or anything in between.

提交回复
热议问题