skip to content | skip to sidebar

name hiding and the using-declaration

This post is inspired by a question posed by a colleague some time ago. Consider following C++ code fragment in which B derives from A and declares a fun with a different signature:

#include <iostream>

struct A
{
	void fun(int x) { std::cout << "int A\n"; }
};

struct B: A
{
	void fun(float x) { std::cout << "float B\n"; }
};

int main()
{
	B b;
	b.fun(3);
	return 0;
}

What shall it print? If your answer is int A, you’re probably assuming that the compiler will choose the best possible overload between A::fun and B::fun. In that case however you’re ignoring an often overlooked rule called name hiding.

The truth is that when you declare a function in a derived class, any function in the parent class that goes by the same name will be hidden from B‘s interface. From the C++ standard (C++ 98, 3.3.7 § 1): “A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.” The consequence is that A::fun will never be considered as a possible overload, and only suitable function to call is B::fun. And so, the program will print float B.

Can it be fixed? Well, it depends on what you mean by “fixed”, as nothing is broken. But it surely is possible to bring back A‘s fun into B‘s interface so that it is considered as a potential overload again. One labourious way to do it is by declaring a new function in B that forwards the call to A:

// laborious solution
struct B: A
{
	void fun(int x) { A::fun(x); }
	void fun(float x) { std::cout << "float B\n"; }
};

However, if there are many fun overloads in A, and many different derived classes like B, this quickly gets very annoying …

A far more easier solution is the using-declaration (C++ 98, 7.3.3 and 10.2 § 2): “A using-declaration introduces a name into the declarative region in which the using-declaration appears. That name is a synonym for the name of some entity declared elsewhere.” It will declare A‘s fun into B‘s scope, and make it as such available as a potential overload:

// easier solution
struct B: A
{
	using A::fun;
	void fun(float x) { std::cout << "float B\n"; }
};

Now the compiler will also consider A::fun as a possible overload and print int A.

There are however a few cases in which the using-declaration won’t help: e.g. you can’t use it on constructors as they don’t have a name. For more details, see section 7.3.3 of the C++ 98 standard.

How To Write Shared Libraries

This post serves as a bookmark for a little gem I’ve found: How To Write Shared Libraries (pdf) by Ulrich Drepper. It’s a 47 page document on writing shared libraries in a Unix environment.

The Next Big Thing, 13 Nov 2008, part 2

Two weeks ago, I reported on the first three bands that hit the stage on The Next Big Thing showcase night by Keremos. So that means you still have three more left. For these bands, I replaced the Sigma 30mm f/1.4 lens by my wide-angle zoom EF-S 17-55mm f/2.8 IS USM. Though two stops slower, it supports image stabilisation (IS) which should in theory compensate for three stops. However, with fast moving targets, that’s not always helpful in practice. On the other hand, it is a zoom giving an extra degree of freedom which is useful if you’re stuck to your position, and its glass is of excellent quality. Still, it’s hard to say which one performed best. Also, a lot of the pics are actually taken with the 100mm prime.

Team William is a funky indie pop band that mixes guitars with something that reminds me of 8-bit game consoles. It has an odd sound that I really like. They played on the small stage: no smoke and steady (mainly reddish) lights. Not perfect, but good enough for some nice shots. Some worked out better in black & white, because of the strong red colourcast.

Team William @ The Next Big Thing, 13 November 2008 (by Bram de Greve)

Team William @ The Next Big Thing, 13 November 2008 (by Bram de Greve)

Kawada is a formation around Joeri Cnapelinckx, playing somewhat more introspective tunes. Listen to Creating a bigger boat for an easy but very enjoyable song, or Fake license for a more fun fanfare-loaded track. They were the last band to ascend the big stage, and again it proofed quite hard to get any decent pics: too dark and too much smoke. Yet, of all bands on this stage, I like their pictures most.

Kawada @ The Next Big Thing, 13 November 2008 (by Bram de Greve)

The last band of the night was Roadburg. Sharing two members with The Galacticos, they are at least as cheerful, albeit jazzier. Which is nice. An old tube radio was featured as prop and apparently it is also very handy to put your drinks on.

Roadburg @ The Next Big Thing, 13 November 2008 (by Bram de Greve)

Roadburg @ The Next Big Thing, 13 November 2008 (by Bram de Greve)

Links to complete photo series: Team William, Kawada and Roadburg.

Python string interpolation trick

Today, I will show you a trick that may help you with the heavier string formatting jobs in Python.

Most people using Python know how to format output using string interpolation. It works much like C’s sprintf, except for the funny % operator:

>>> a = "the Knights"
>>> b = "Ni"
>>> "we are %s who say %s" % (a, b)
'we are the Knights who say Ni'

What is less known is that you can replace the right hand argument by a dictionary and use its keys to tell what value goes where. That’s handy when you need to insert more than one or two in a string, or when a single value is used more than once. Especially for localisation strings, it gives you the flexibility to determine the when and where of the values in the string itself.

>>> "we are %(who)s who say %(what)s" % {"who": a, "what": b}
'we are the Knights who say Ni'

Combine that with the even lesser known function vars() which returns a dictionary of the local variables and their values:

>>> vars()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
 '__main__', 'b': 'Ni', '__doc__': None, 'a': 'the Knights'}

Use it at the right hand, and you can simply use the variable names as keywords:

>>> "we are %(a)s who say %(b)s" % vars()
'we are the Knights who say Ni'

This probably is the closest you can get to the Perl and PHP style syntax "we are the $a who say $b".

Share and Enjoy!

The Next Big Thing, 13 Nov 2008, part 1

Last month, I went to The Next Big Thing at Trix, a showcase night presented by Keremos. Six promising young bands on the rise populated the bill: The Galacticos, Arquettes, Steak Number Eight, Team William, Kawada and Roadburg. That much good on one night was a perfect opportunity to go and shoot some pictures. For the following three bands, I’ve been using two prime lenses: a Sigma 30mm f/1.4 and a Canon EF 100mm f/2.0.

The first band to hit the stage was The Galacticos: always in for a good show with happy rock and catchy melodies. In contrast to last time at Tank series, there was enough light at Trix to justify colour pictures. Though the excessive use of smoke made it quite difficult still. [photo series]

I’ve seen Arquettes playing before, but this was the first time I had my camera with. Though they played on the smaller stage with basic lights, I’m much more pleased with the results than of The Galacticos and Steak Number Eight. Is it the lack of smoke? Or the more static lighting flooding the stage? I’ve done quite a few black & white conversions as the red front lights almost always result in very ugly colour casts, even if you set your white balance at minimum colour temperature and drag down the red channel. But in some cases it was still acceptable. (photo series)

Next on the main stage was Steak Number Eight. I’ve seen them a number of times now and their slow and heavy wall of sound still impresses me. The seemingly mandatory thick cloud of smoke made it quite impossible to get the crisp shots I was after. And so I decided to play it a bit differently: desaturating the colours even further and upping the contrast, building up a grim atmosphere that fits their music. (photo series)

Links to complete photo series: The Galacticos, Arquettes, Steak Number Eight.

manuals on software optimization

While looking for some info on 64 bit masm, I stumbled on Agner Fog’s site with five comprehensive manuals on software optimization. Each covers a different topic: optimizations in C/C++ and assembly, microarchitectures, instruction tables and calling conventions.

Dollarqueen @ Du Gaz, 8 Nov 2008

After the After All gig, I rode back with Piet Focroul to Du Gaz in Bruges, where Dollarqueen was giving a show: a fairly new band but with experienced musicians, pure rock ‘n roll with a touch of blues and a hint of The Doors. Their songs just get stuck in your head (my personal favourites: The bomb and Rock ‘n roll star).

The light conditions couldn’t have been more different than on Blikfest: static and dim. Perfect conditions to try out my new Sigma 30mm f/1.4. And with a crowd as packed as in Du Gaz on that night, the wide angle 30mm was very welcome (well, technically, due to the crop factor, it actually is equivalent to the standard FOV).

Slow shutterspeeds, lots of motionblurred pictures, ugly colours only suitable for black and white. And yet, somehow I love it! I’m especially fond of the following detail of a microphone waiting on the floor with behind it a pair of feet tapping on the rhythm of the music:

Kudos to Piet for the hospitality and introduction to this wonderful band!

link to complete photo series

After All @ Blikfest, 8 Nov 2008

A couple of weeks ago, I accompanied After All to a gig for the second time, , . And just like the previous time, it was a great experience. The venue was JC Togenblik in Beveren-Waas, Belgium of course, where the fifth edition of the Blikfest metal festival was organized. I didn’t see much of the rest of the festival, except for Thurisaz of which I also have some pictures. My only interest was to capture the trash metal attack from Bruges.

It must have been one of the first times I didn’t have to battle against dim lights. Apart maybe for the Waxdolls gig. Most shots had comfortable fast shutterspeeds. Fast enough to use my telelens and still get crisp shots of the drummer. The lack of smoke did help as well of course. There’s nothing that ruins a distant shot like a thick cloud of smoke.

So, plenty of technically successful shots to go home with, and many have ended up in the gallery. Maybe too many. But somehow, I can’t help feeling they don’t have quite the same ring as my other shots. It’s almost like they are too clean … No smoke gives you crisp shots, but at the same time you loose colourful backgrounds. Instead, you get a dull black hole. Still, I have one favourite shot, which is this black and white one:

At any rate, I had a great time, begging for more. Here’s to After All, thanks for the hospitality and the ride, it is much appreciated!

link to complete photo series

Waxdolls @ Vijverpop 2008

The night after Best of the West (see previous post), two members of QuiOui performed at Vijverpop as the Waxdolls. They are called the electropunk revelation of Bruges or Ghent (depends on who’s reporting it =), and they definitely have a bright future ahead.

Waxdolls @ Vijverpop 2008 (by Bram de Greve)

Vijverpop is a festival organised by a school in Bruges. Though it is a very small festival, it cleary showed what difference it makes when there’s enough light and a bit of smoke. Enough light means you can shoot at faster shuttertimes and have more pictures that are sharp and well exposed. With a bit of smoke, you get nice colours all over your image instead of ugly walls and ceilings.

Being very pleased with the colours, I didn’t even try to convert to black and white. Instead, I even increased the local contrasts and colour saturations to give it extra punch. It does fit the electropunk better, I think.

link to complete series

Best of the West …

It’s been exactly two months ago that I’ve followed QuiOui for a second time to a concert. But somehow, I’ve only now managed to get all pictures online … Place to be was JC Ten Goudberge in Wevelgem. The concert bill featured all medallists of Westtalent 2007: Low Vertical, QuiOui and Steak Number Eight.

QuiOui @ Best of the West, 9 May 2008 (by Bram de Greve)

The lighting conditions were difficult at best. Two stands with four spots each in impossible locations with colour combinations of which I didn’t know they existed. For Steak Number Eight, they altered it to mainly reds with some spots on the floor behind the drums. Which was actually very cool to see, but possibly even worse for picture taking. The f/2.8 of my 17-55mm was just too slow, and the 50mm f/1.8 was constantly hunting for focus. So at the end, taking pictures was more a matter of sheer luck.

Steak Number Eight @ Best of the West, 9 May 2008 (by Bram de Greve)

Though the pictures aren’t great in an absolute sense, there are still some that I really like. Most of them are very low key and have to build upon atmosphere rather than sharpness. In case of Steak Number Eight, I’ve tried to exaggerate this by applying an unsharp mask with a very large radius to the lightness channel to increase the local contrast.

Links to complete series: Low Vertical, QuiOui, Steak Number Eight.