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

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

For example:

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

Expected output:

int
21条回答
  •  Happy的楠姐
    2020-11-22 02:18

    Very ugly but does the trick if you only want compile time info (e.g. for debugging):

    auto testVar = std::make_tuple(1, 1.0, "abc");
    decltype(testVar)::foo= 1;
    

    Returns:

    Compilation finished with errors:
    source.cpp: In function 'int main()':
    source.cpp:5:19: error: 'foo' is not a member of 'std::tuple'
    

提交回复
热议问题