C++Next is happy to republish the following article by Scott Meyers, with Scott’s permission, of course. Thanks, Scott!
If you write “
int i;” in C++,
i’s type seems obvious:
int. If you write “
const int i;”,
i’s type seems equally obviously to be
const int. Often, these types are exactly what they seem to be, but sometimes they’re not. Under certain circumstances, C++ treats non-const types as const, and under others it treats const types as non-const. Understanding when consts appear and disappear lends insight into the behavior of C++, and that makes it possible to use the language more effectively.This article examines various aspects of type declaration and deduction in both current standard C++ as well as the forthcoming revised standard (C++0x), with an eye towards helping you understand how and why the effective type of a variable can be different from what’s “obvious.” You’ll find the article most useful if you are familiar with the basic rules for template argument deduction, are aware of the difference between lvalues and rvalues, and have had some exposure to the new C++0x features lvalue and rvalue references,
auto variables, and lambda expressions.
Continue Reading ▶