decltype() as a documentating helper…

I just realized that since I’m using VC++ 2010 Beta 2 and thus, can enjoy some of the C++ 0x features, I tend to often use the decltype for documentation reason, not just in template code. “How come?”, I hear some of you say.

The following code compiles but nowhere does it say that I picked the type of m to be int because the parameter is type int:


void foo(int i) {

   // …

   int m = 10;

   // …

}


And most people would not care writing a comment next to the declaration of m to explain that. That’s why I like writing that code like this:


void foo(int i) {

   // …

   decltype(i) m = 10;

   // …

}


 

And of course, if you decide to change i’s type to __int64 later on, the type of m will also change automatically to __int64.

 

Of course, by design, i and m must have the same semantic, the same type…

 

Tchau.

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

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