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 beclass C {
int i;
explcit C (int v) { i = v; }
};
mutable
The keywordmutable
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 asvolatile
no access to the variable is served by a previously stored value in a register. Declare like this:volatile int i;
auto
Anauto
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 theexport
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 aboutsizeof()
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 usetypename
and class
keywords to indicate type parameters. Thus, the following two are equivalent:Template <class T> …
Template <tyepname T>…
Checkout c++0x. The keyword auto has be re-purposed to do basic type inferencing. All the well known C++ compilers already implement it.
ReplyDeleteBut other than that, are these really least known ? I would consider them pretty basic and hard to do without, if someone is writing anything remotely useful code. Well, apart from maybe one or two.
Nice write up, but I think there's quite a lot of important information missing here.
ReplyDeleteauto - Should probably mention that the meaning of 'auto' in C++0x is changing completely. In the next standard, it indicates that the type of the variable should be inferred from the expression. e.g.
auto x = 5;
int x = 5; //equivalent
register - Should probably mention that (like inline) it is usually ignored by the compiler because the compiler is usually better at making the decision for you.
typename - A far more common use of typename is as a disambiguator for templates, e.g.
template
It find(It first, It last, It::value_type value) // error, need typename
template
It find(It first, It last, typename It::value_type value) // error, need typename
The ones I didn't know: explicit, mutable, export, typeid, typename.
ReplyDeleteAmong the ones I've used, I value the 'extern' one a lot, partly because I've had to work a lot with mixing C and C++ projects, and sometimes, treating C code as C++ code just doesn't work.
Hi,
ReplyDeleteU really seem to be into C/C++ n Linux these days. Whats up? Working on anything specific?
What else do you think I should write about? Top 10 Bollywood movies? :)
ReplyDeleteIt's an experiment; you can find some results using topsy: http://topsy.com/www.thejaywalker.net/2010/12/difference-between-posix-and-standard-c.html and http://topsy.com/www.thejaywalker.net/2011/01/least-known-c-keywords.html
Never came across such keywords.. thanks for sharing
ReplyDelete