Podcasts: Scott Meyers on C++0x/Johan Bezem on Misra
Software Engineering radio recently had two episodes which are of particular interest to the C/C++ developer.
First Scott Meyers gives a nice high-level overview on the features of the upcoming C++0x standard covering Lambda expressions, Uniform initialization, Initializer lists, threading and Variadic templates amongst others. Wikipedia has a good article covering C++0x in more detail. The episode is quite entertaining not only because Scott is a good speaker but also because Markus, the host of the podcast (a Java-guy), apparently has several C++WTF moments. Link - Episode 159 C++0x.
The second episode is with Johan Bezem who is talking about MISRA which is a set of guidelines for C/C++ development promoting safety and reliability. MISRA was developed and still is mainly in use in the automotive industry. To be frank the episode isn’t as entertaining as the one with Scott but i finished it because it provides an interesting perspective on C development for small/deeply embedded systems. Link - Episode 152 MISRA.
Eclipse CDT language mappings
Eclipse/CDT fails to parse some file? You don”t see anything wrong with it, neither does the compiler? Still CDT underlines parts or all of a file to mark a parse error? The most probable cause is that your file has a file extension that CDT associates with the C language, but it contains some bits of C++.
To solve this problem the CDT offers a feature called language mapping which allows you to override the default for a given file type. This can be done for a single file, for the project or for the whole workspace.
For a single file, right click on a file in the project browser -> select Properties -> C/C++ General -> Language Mappings. The dialog also offers some shortcut to do it on project or workspace level.
Adding new sites to the eclipse updater “hangs”
Just for the record: If adding a new site to your eclipse updater seems to hang/times out (entry is always “pending…”), its most probably not a problem with the update site itself but rather some firewall interfering. Make sure you have setup the appropriate proxies in eclipse (Window->Preferences->General->Network Connection).
Installing a gcc4.4 snapshot on Ubuntu
The upcoming gcc4.4 release adds a few more C++0x features (initializer lists, auto typed variables, defaulted and deleted functions, strongly typed enums, …) to the in this regard still very incomplete g++. To be able to play with it one has to compile a gcc-snapshot which turned out to be fairly easy.
- Download a copy of the gcc-core and the gcc-g++ snapshot from here. I used gcc-core-4.4-20090306.tar.bz2 and gcc-g++-4.4-20090306.tar.bz2
- Extract both archives into the same directory.
tar xvjf gcc-core-4.4-20090306.tar.bz2 && tar xvjf gcc-g++-4.4-20090306.tar.bz2 && cd gcc-4.4-20090306 - run
configure ./configure --prefix=$SOMEPLACEBe sure not to overwrite your default gcc-installation. If anything is missing configure will complain. - On my Ubuntu system i had to install libmpfr-dev to proceed.
sudo apt-get install libmpfr-dev - Now all it takes is time…
make && make install
To use it just put $SOMEPLACE in front of your $PATH. If you are using it to compile code which uses C++0x features (which is the whole point) you have to add -std=c++0x to your compiler command line. Bonus: If you are using Eclipse/CDT you can start it with the modified path and the builder will pickup the new gcc just fine.
Eclipse CDT 6 Milestone 5
The final release of CDT6 is still a few month ahead (scheduled for ~June) but there are already public milestone builds available. I haven’t played much with M5 but one thing that directly caught my attention is that you no longer have to manually setup all the include paths in your project. Eclipse will just use all the headers inside your workspace to resolve includes.
This is something i have been longing for for years. If you work inside a large projects tree with a lot of include directories (think public interfaces/private headers of N modules) its really painful to add all these to the project.
It will also enhance the out of the box experience for people importing their first project into eclipse. I know from experience that most people don’t know that they have to setup the include paths first (how should they? before CDT5 the default setup was not even outlining missing includes!). Without being able to resolve the includes the indexer will not work properly so all the advanced features are not available (go to definition, type hierarchy, global symbol lookup, macro expansion, include browser, etc…). Confronted with such a crippled project first timers might jump to the conclusion that eclipse has not much more to offer than their trusted editor of choice.
As always i am looking forward to the full release.
Elegant human readable enums
While reading the source of some FOSS project i noticed that the authors have come up with a nice way to get human readable enums through the use of the “stringification feature” of the C/C++ preprocessor. The trick is that the members of the enum are declared through a macro which can be expanded in different ways.
-
#include <iostream>
-
#include <cstdlib>
-
#include <cassert>
-
#include <time.h>
-
-
namespace MyStuff
-
{
-
enum Things {
-
#define THING(X) X,
-
#include "things.def"
-
MAX_THINGS
-
};
-
-
static const char* const ThingNames[] = {
-
#define THING(X) #X,
-
// ^ preprocessor stringification
-
#include "things.def"
-
NULL
-
};
-
-
const char *toString(Things t) {
-
assert(t >= 0 && t < MAX_THINGS);
-
return ThingNames[t];
-
}
-
}
-
-
int main(int argc, char **argv)
-
{
-
using namespace MyStuff;
-
std::srand(::time(NULL));
-
for (int i = 0; i < 4; ++i) {
-
Things aRandomThing =
-
static_cast<Things>(std::rand() % MAX_THINGS);
-
}
-
}
-
things.def:
-
-
#ifndef THING
-
#define THING
-
#endif
-
-
THING(CELLPHONE)
-
THING(NOTEBOOK)
-
THING(CUP_OF_TEA)
-
THING(MOUSE)
-
-
#undef THING
-
Time after time i am amazed what you can do with some of the advanced preprocessor features like “token pasting” or stringification.
Overload
I recently discover “Overload” - a publication done bi-monthly by the ACCU (Association of C & C++ Users). It’s focus is on C++ and software engineering in general. The overall quality of the articles is quite good and they also have some well known names like Kevlin Henney or Peter Sommerlad writing articles for them. In my opinion a worthy alternative to the dearly missed “C/C++ User Journal”. The complete archive can be found here. Highly recommended!