Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Sunday, January 09, 2011

Least Known C++ Keywords

This posts goes into the purpose and usage of some C++ keywords that rarely make it to the source code except of experienced programmers.

explicit

explicit is a function-specifier; other function specifiers are inline and virtual. The keyword explicit is used with a constructor to mention that implicit type conversion for a class shouldn't be performed by the compiler. Consider the following class whose constructor takes an integer as its single argument:

class C {
   int i;
   C (int v) { i = v; }
};


Now, let's suppose that you have a method with the following signature:

void f (C o) {
   // do something in your source code
}


And you call this function as f(5). C++ will automatically convert the passed argument 5 into an object of type C by invoking C's constructor with value 5. explicit keyword stops this kind of behavior. So your class definition could be
class C {
   int i;
   explcit C (int v) { i = v; }
};


mutable

The keyword mutable is related to member variables and const-functions; it's a storage-class-specifier just like auto, register, static and extern. The semantics of const-functions is that they cannot modify class member variables. Consider the following code which fails to compile:
class C {
   int i;
   void f () const {
      i = 5;
   }

};

This fails because f() is assumed not to change any member variable's value. However, if you add mutable keyword to the variable i this works!
class C {
   mutable int i;
   C () {
      i = 0;
   }
   void f () const {
      i++;
   }
};


This could possibly be used to keep track of how many times a function is invoked, for example.

volatile

This keyword asks the compiler not to optimize access to a certain variable. Such undesired optimization could happen, for example, when a variable could possibly be modified by an external interrupt or a different thread. For optimization's sake if a variable's value is copied to a process register, next access could be made faster by avoiding the memory trip and by accessing the register value. However, if you qualify the variable as volatile no access to the variable is served by a previously stored value in a register. Declare like this:
volatile int i;

auto

An auto variable is visible only in the block in which it is declared, and has a local lifetime. This keyword is not used in practice because all variables are implicitly declared as automatic. Thus, auto int i and int i are the same.

export

If you have worked with reusable templates, you know that you have to keep both the definition and implementation of a template in a header file. This happens because code against a template is never gerenated by a compiler unless template instantiation with a particular type is done within the program being compiled. The best you can do to separete code and definition (for separation of concerns) is to use multiple header files, each containing some part of the template. What you can't do (without having the export keyword) is to use a .cpp file to place the implementation part of a template. While this explains the usage of export, the problem is that not many compilers support it.

extern

extern is a linkage-specifier. The extern keyword tells the compiler to "look somewhere else" for the definition of a symbol. Where extern is used, a symbol is just declared. extern can also be used to tell the compiler that a symbol has non-C++ linkage. For example, the following statement indicates that g() is a C language function:
extern "C" int g().

register

It's an optimization request to use a register to store the value of a variable. Your compiler may decline to honor this request.

sizeof

While almost everyone programming in C/ C++ knows about sizeof() but only the minority knows that it's an operator (and not a function). It gives you the size of a variable.

typeid

With Runtime Type Identification (RTTI), typeid allows a C++ program to retrieve the actual derived type of an objected referred to by a pointer or a reference. It returns a reference to std::type_info object. You can get the object name as a string by calling typeof(myobject).name().

typename

No, this has nothing to do with RTTI. With template definitions, you can interchangeably use typename and class keywords to indicate type parameters. Thus, the following two are equivalent:
Template <class T> …
Template <tyepname T>…

and, or, etc.

These are operators corresponding to &&, ||, etc. You can either use the symbols or the complete keywords.

How many of these did you already know? Which ones have you practically used in your programs?