The function of printing an expression type

Very often I see debates on forums about the type that this or that expression will have. So, I decided to make a little note in the blog to refer to an example of code that prints the type of an expression and information about it:

1813j2

#include <iostream>
using namespace std;
template <typename T>
void PrintTypeInfo(char const* description, T)
{
  const type_info &info = typeid(T);
  cerr << "\"" << description << "\":"
       << " type = " << info.name()
       << "; 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

Written by Andrey Karpov.


			

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s