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

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

For example:

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

Expected output:

int
21条回答
  •  遥遥无期
    2020-11-22 02:26

    Note that the names generated by the RTTI feature of C++ is not portable. For example, the class

    MyNamespace::CMyContainer
    

    will have the following names:

    // MSVC 2003:
    class MyNamespace::CMyContainer[int,class test_MyNamespace::CMyObject]
    // G++ 4.2:
    N8MyNamespace8CMyContainerIiN13test_MyNamespace9CMyObjectEEE
    

    So you can't use this information for serialization. But still, the typeid(a).name() property can still be used for log/debug purposes

提交回复
热议问题