The function of printing an expression's type

Andrey Karpov
Articles: 565

I very often meet debates in forums on what type this or that expression will have. So I decided to make a little note in the blog to refer to this.

An example of code printing the type of an expression and information about it:

#include <iostream>
using namespace std;
template <typename T>
void PrintTypeInfo(char const* description, T)
  const type_info &info = typeid(T);
  cerr << "\"" << description << "\":"
       << " type = " <<
       << "; sizeof = " << sizeof (T)
       << "; alignof = " << __alignof (T)
       << endl;
int _tmain(int, _TCHAR *[])
  char c1 = 0, c2 = 0;
  PrintTypeInfo("char + char", c1 + c2);

The result:

"char + char": type = int; sizeof = 4; alignof = 4

Use PVS-Studio to search for bugs in C, C++, C# and Java

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.

goto PVS-Studio;

Andrey Karpov
Articles: 565

Bugs Found

Checked Projects
Collected Errors
14 526
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →