Skip to main content

Posts

Showing posts from February, 2007

More C++ links

Danny Kalev, a former member of the C++ standards committee, posts really interesting updates about C++0x (now most likely C++09) on his blog hosted by informit.com . I particularly like this one.

Service Configurator pattern and singleton deletion

The Service Configurator pattern is about linking in additional functionality in an application dynamically by means of an API such as dlsym/dlopen provided by the OS/run-time linker. Almost all popular platforms provide this functionality. This post is about programmatically, dynamically loaded libraries (hence forth called simply library)(e.g., .DLL, .so) that contain sigletons. One of my previous posts talks about singleton deletion. If the sinlgleton in such a library is a static instance then it is not deleted untill the library is closed using appropriate API such as dlclose. This can be quite undesirable or awkward at times. All the destruction techniques based on static variables fail if you want to destroy the singleton without unloading the library. A possible variant of the singleton in such a case is given below. class Singleton { protected: Singleton (); public: static Singleton *instance () { if (instance_ == 0) instance_ = new Singleton; return instance_; } static void

swap using XOR

In one of my previous posts I described a few ways of swapping two numbers using xor operation in a sinlge line. The discussion is not correct or rather incomplete in the sense that the algorithm needs to be guarded by an if condition. The algorithm that exercises proper care of sequence points is as below: swap (int &a, int &b) { if (&a != &b) { a ^= b; b ^= a; a ^= b; } } It is crutial that the two number must not be located at the same address. The two numbers must have different identities. The algorithm works even though the values are the same.

Publications by C++ experts

Articles by experts are saught after the most. Therefore, here are the links to the articles written by some of my favorite authors (in no particular order). Bjarne Stroustrup Jaakko Jarvi Scott Meyers Steve Dewhurst Andrei Alexandrescu Douglas Schmidt Herb Sutter Kevlin Henney Matthew Wilson Dan Saks Robert C. Martin Chuck Allison Andrew Koenig Nathan Myers James Coplien Stan Lippman ( blog ) John Vlissides Danny Kalev Angelika Langer Christopher Diggins Thomas Becker

Ownership idioms, The Move Constructor idiom and auto_ptr

There are properties of objects that are relevant beyond mere identity of the object. One of such properties is Ownership! Who owns the object? Can you transfer the ownership? Questions like these become quite important when you are implementing Resource Acquisition Is Initialization (RAII) idiom. Tom Cargill describes [in PLoPD2] a set of ownership patterns: "Creator as a Sole Owner", "Sequence of Owners", and "Shared Ownership". Lets take a look at the idioms for each one of them. Creator as a sole owner can be simply implemented using const auto_ptr idiom by Hurb Sutter. Sequence of owners come into play when ownerships moves from one guy to the other. (a.k.a. Move Semantics). Standard auto_ptrs use move semantics. Implementation of move semantics appears to be a juicy issue as far as std::auto_ptrs are concerned. See Meyer's errata like notes on his book More Effective C++ and Sutter's GotW #25 articles. M. D. Wilson's article on Move