Programmer’s Corner: A Rationale for C++0x’s Rvalue-References

December 18th, 2010 No comments

Arguably one of the more advanced features of the upcoming C++0x standard are the so-called rvalue-references. A few months ago, we were discussing an example of what unpleasant issues one can run into in a world without rvalue-references. In this article, we will try to give a more extensive rationale for why we need another type of references in C++ and how they are specified in the current draft of the standard.

Why References?

When it comes to passing arguments, most modern programming languages already made the choice for you: Built-in types are passed by value, complex types by reference. In C++, things are more complicated, as a programmer may choose between passing by-value, by-pointer or by-reference.

The first two are natural heritage from C. References may seem kind of redundant at a first glance, since they  behave almost exactly like (const) pointers from a semantic point of view. And indeed the initial motivation for references came from a purely syntactic problem, namely that of operator overloading for complex types:

class MyType {
 ...
public:
  MyType operator+(MyType& rhs) const;
};
 
MyType a, b, c;
a = b + c;

This natural way of using operators can not be achieved with pointers alone (which would require explicit address operators on the caller’s side) . While there are further differences between pointers and references, this remains the most prominent example of why the low-level concept of raw-memory pointers is no longer sufficient in the context of higher-level C++ code.

References and Rvalues

However, the initial implementation of References had a severe problem, as it allowed binding to rvalues:

// side-effect: increments the passed variable
void increment(long& l) {
   ++l;
}
 
[...]
int i = 41;
increment(i);
//expected: i == 42

This is actually a very subtle error. Since C++ inherited the complex integer conversion rules from C, the compiler may easily resolve the call to increment(), by implicitly casting the int to long, creating an unnamed temporary (i.e. an rvalue) in the process. However, all side effects produced by the function call now get applied to that unnamed temporary instead of the initial int, so the function really has no effect on the int at all.

In order to avoid this pitfall while maintaining the powerful syntax of references, the 2.0 release of C++ from 1989 prohibited binding of non-const references to rvalues. This is the behavior that is still in use today and this is also the behavior that caused trouble in the previous article.

Introducing: Rvalue-References

In C++98, we distinguished between pointers (raw memory addresses), references (alias for a named value) and the special case of const-references (same as reference but may bind to an unnamed value as well).

As it turns out, it is indeed required to introduce another type of references, a non-const reference that binds to unnamed temporaries and unnamed temporaries only: Rvalue-references. Actually, the initial draft allowed rvalue-references to bind to lvalues as well, but that turned out to cause more trouble than good, so by the current draft, rvalue-references may indeed only bind to rvalues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MyType {
public:
  /* Constructor
   */
  MyType(int dummy);
  /* Copy constructor; uses const-lvalue-reference
   */
  MyType(MyType const& t);
  /* Move constructor; uses rvalue-references (new in C++0x)
   */
  MyType(MyType&& t);
};
 
int main()
{
    MyType a(42);   //construction
    MyType a_copy(a);  //copy construction
    MyType a_moved(MyType(42));  //move construction from rvalue
}

Before we take a look at some of the actual use-cases for this feature, we need to cover one subtle yet very important detail: Although an rvalue-reference ever only binds to rvalues, it is itself an ordinary lvalue as long as its named. Consider the following function:

void take_by_rvalref(MyType&& p)
{
    MyType p_copy(p);  // *no* move construction!
}

Although the parameter is passed as an rvalue-reference, p_copy is constructed using the copy-constructor, since p is itself an lvalue. This is similar to how a const reference always behaves like a (const) lvalue, even if it was bound to an rvalue by the caller.

In certain situations, it may be desirable to treat p as an rvalue to enforce invocation of the move-constructor, which we will cover below.

Move Semantics

With rvalue references, we can resolve the problem from our last article. Assuming we have an object of class Guard that is responsible for freeing a resource upon destruction, we ran into trouble when trying to write a factory method that returns resources encapsulated by Guards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Guard {
private:
	int resource_;
public:
	Guard(int r)
		:resource_(r)
	{
		::std::cout << this << " Guard Constructor" << ::std::endl;
		::std::cout << this << " *** Guarding " << resource_ << ::std::endl;
	}
	~Guard()
	{
		::std::cout << this << " Guard Destructor" << ::std::endl;
		if(resource_ > 0) {
			::std::cout << this << " *** Free " << resource_ << ::std::endl;
		}
	}
	Guard(Guard& rhs)
		:resource_(rhs.Detach())
	{
		::std::cout << this << " Copy constructor; Assuming ownership of " 
					<< resource_ << ::std::endl;
	}
private:
	int Detach()
	{
		::std::cout << this << " Detaching " << resource_ << ::std::endl;
		int ret = resource_;
		resource_ = 0;
		return ret;
	}
};
 
class Factory {
public:
	Guard BuildGuard()
	{
		return Guard(42);
		//error: can not copy-construct from unnamed temporary Guard
	}
};

With rvalue-references, we resolve the issue by introducing a move constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Guard {
	[...]
public:
	Guard(Guard&& rhs)
		:resource_(rhs.Detach())
	{
		::std::cout << this << " Guard Move Constructor" << ::std::endl;
		::std::cout << " Assuming ownership of " << resource_ << ::std::endl;
	}
};
 
class Factory {
public:
	Guard BuildGuard()
	{
		return Guard(42);
		//fine: invokes move-constructor Guard(Guard&&)
	}
};

Actually, on both VC2010 and gcc 4.3.4 this makes RVO kick in, so you probably won’t see the output from the move-constructor on the console.

Another use for move semantics is the elimination of spurious copies. Let’s assume for a moment that copy constructing a Guard would be horribly expensive, so things like the following become undesirable:

1
2
3
4
5
6
7
8
9
10
Guard BuildGuard()
{
	Guard g(42);
 
	//do some fancy stuff with g
	// [...]
 
	return g;
	//bad: implicit copy construction
}

Unless we get lucky with the compiler, we will have to pay for the copy-construction here. This is very annyoing, especially regarding that the local source of the copy will be destroyed immediately after the copying is finished. In C++98 the usual workaround for this would be to pass a Guard* as output argument to the function. However, no one likes output arguments, as they are clumsy to use and make your code hard to read.
In this case it would be nice to force the usage of the move constructor. Since moving allows to change the state of the source argument, it can be implemented very efficiently. For this case C++0x offers the std::move() function which converts its lvalue argument to an rvalue:

1
2
3
4
5
6
7
8
9
10
Guard BuildGuard()
{
	Guard g(42);
 
	//do some fancy stuff with g
	// [...]
 
	return ::std::move(g);
	//force move construction
}

Obviously, std::move() should only be used with extreme caution. It’s best to think of it as a special form of typecast that invalidates its source argument.

A nice property of move-semantics is, that we may omit the copy-constructor of an object altogether (i.e. specify a privately declared, unimplemented copy-constructor), creating types that are movable, but not copyable.

Perfect Forwarding

The last use is perhaps the most subtle one. Most C++-programmers will probably never need to make use of this feature directly, so feel free to skip this section if your head is already spinning with confusion.

Let’s return to our factory example once more and assume that Guard is to be constructed from some other object:

1
2
3
4
5
6
7
8
template<typename T>
Guard BuildGuard(T const& obj)
{
	Guard g(obj);
	// binds only to Guard(T const&)
	// [...]
	return ::std::move(g);
}

This works fine as long as the constructor of Guard is expecting a T const&. If for some reason it only accepts a non-const reference, we need to once again fall back to rvalue-references to allow passing of unnamed temporaries to BuildGuard():

1
2
3
4
5
6
7
8
template<typename T>
Guard BuildGuard(T&& o)
{
	Guard g(o);
	// also binds to Guard(T& o)
	// [...]
	return ::std::move(g);
}

The obvious disadvantage here is that the factory method now no longer works with lvalue arguments, at least not without an explicit std::move() on the caller’s side.
The only way to make it work for both cases is to implement two factory methods, which will result in an exponential interface blowup once the number of arguments increases.
Usually, this is not a problem, as Guard is likely to support only one of the two constructor types and we know beforehand which one that is going to be. Things only get ugly when Guard itself is a templated type, so we can not make any assumptions about its interface.
In order to handle this case, we need a way to preserve the const-ness of the argument bound to the rvalue-reference. This is taken care of by the std::forward() function-template:

1
2
3
4
5
6
7
8
9
template<typename T>
Guard BuildGuard(T&& o)
{
	Guard g(::std::forward<T>(o));
	// binds to either Guard(T&) or Guard(T const&),
	// depending on the type of the value bound by o
	// [...]
	return ::std::move(g);
}

One interesting side note about std::forward is that it only works with templated parameters: If the function is passed an unnamed temporary of type Foo, T resolves to Foo; if it is however passed an lvalue of type Foo, T resolves to Foo&! So the factory method in this case is actually passed (hold your breath!) an rvalue-reference to an unnamed temporary of type non-const lvalue-reference to Foo.
The std::forward function depends on this behavior of the template type deduction system to work correctly.

Conclusion

In this article, we discussed some of the decisions that determined the way references in C++ work today and gave a motivation for a new type of rvalue-references. We also gave a quick overview over some prominent uses for this new feature.
To conclude, let us take a quick look at what the most important benefits of rvalue-references are: First, they allow the implementation of movable types, which takes a lot of scare out of auto_ptr-like types. Also, move constructors eliminate a number of causes for spurious copies.
The main target audience for this feature are undoubtedly library developers. Even if you don’t change a single line of code, recompiling code that makes heavy use of the STL on an implementation that fully supports rvalue-references may result in a notable performance boost.

Literature:
Stroustrup, BjarneThe Design and Evolution of C++ (Addison Wesley)
Hinnant, Howard E.; Stroustrup, Bjarne; Kozicki, BronekA Brief Introduction to Rvalue References (N2027)

Categories: Programming

Benyamin Nuss Plays Uematsu

September 27th, 2010 No comments

[youtube oDcH2NPcn6I]

It’s a great time to be a game music enthusiast in Germany. The third installment of the Symphonic * concert series in Cologne last week was once again a tremendous success. As last year, the WDR Rundfunkorchester did a marvelous job at interpreting classic game tunes and as last year, they were again joined by percussionist Rony Barrak and German piano prodigy Benyamin Nuss. The very same Benyamin Nuss has just recently released a record under the renowned Deutsche Grammophon label, interpreting game soundtracks by world-famous Square Enix composer Nobuo Uematsu on the piano. Currently, Nuss is touring Germany and will also give performances in Luxembourg and Japan later this year. This author attended the second concert of the tour last Saturday in Trier, Germany.

Only two days separated the concerts in Cologne and Trier, yet they could hardly have been more contrasted. On the one hand we had the shiny Philharmonie in Cologne, completely sold out with more than two thousand attendants and a full fledged orchestra plus choir. On the other hand the cozy indie-charm of the Tufa Trier, with an audience of little over fifty people listening to a single piano on the unadorned stage. And yet, from a purely musical point of view, Nuss managed to deliver a solo-performance that was at least as fulfilling as that by the full-fledged orchestra in Cologne.

The most remarkable characteristic of Germany’s video game concerts is their willingness for experimentation. Most of the famous video game concerts out there, most prominently the American Video Games Live concerts, but also most of the Japanese orchestral arrangements, try to reproduce the original melodies as accurately as possible. Upon transposing the tracks from their 16-bit synthesizer roots to an orchestral score, they add nothing to the original track besides the increased number of participating instruments. While this still makes for some excellent fan-service, it delivers little beyond that point. In particular, those concerts tend to be utterly disappointing for anyone not extensively familiar with the original works.

A unique peculiarity of the Cologne video game concerts has always been their urge to go beyond that mere reproduction value and incorporate new influences and styles to truly elevate the compositions to a new level. Nuss’s latest work was unmistakably created in the same mindset and pushes the boundaries of musical versatility even further than its orchestral counterparts. Arranged by Japanese Shiro Hamaguchi, Russian Alexander Rosenblatt, American Bill Dobbins and of course Finnish Jonne Valtonnen, the record combines influences ranging from impressionistic music to jazz and classic modern, resulting in what is probably the most colorful and artistically pleasing contribution to the field of video game music so far.

Of course not everyone will like this. Look at the user-comments for Nuss’s performance at Symphonic Fantasies on YouTube and you will find an alarmingly high number of comments mistaking the jazzy disharmonies in Valtonnen’s arrangement of Dearly Beloved as actual playing errors. Maybe the gaming community is not ready to experience their music rearranged on this level of artistic subtlety. Or maybe the internet just makes gamers look stupider than they actually are… again.

Whatever it may be, if you have even the slightest interest in video game music as an art-form, there is absolutely no excuse for missing this seminal piece. Nuss Plays Uematsu is to be considered one of the most remarkable releases in the field, on par with the earliest orchestral arrangements of game music from Japan in the 1990s.
If you are just looking for some easy-listening fan-service, you will probably be disappointed. If you however take your time to get used to this exceptional piece of work and give it a chance to sink in, you might get a lot more than you might have expected.

The record is available for international purchase through the usual channels. Also, be sure to check out the tour dates for the live concerts, they are definitely highly recommended.

Categories: Games, Music

Movie Review: The Wild Hunt

September 12th, 2010 No comments

Playtime

The Wild Hunt
Directed By Alexandre Franchi
Starring Ricky Mabe, Mark Antony Krupa, Kaniehtiio Horn
The Wild Hunt at the imdb

Watched at the Fantasy Filmfest 2010 in Munich.

Contains light spoilers.

Ever since the release of the Lord of the Rings movies, the genre of medieval fantasy has experienced a boom in all areas. Arguably one of the more extreme manifestations of this trend being Live-Action-Role-Play (LARP), a crossbred between historical reenactment and pen-and-paper role-play. In The Wild Hunt your typical twenty-something Erik stumbles into a large-scale LARP in search for his girlfriend. Despite his brother being among the players, Erik has no sympathy for the people lost in the melange of medieval chivalry, norse mythology and pseudo-Shakespearean colloquies, and is completely unwilling to comply with the rules of the game. Eventually this leads to the unavoidable escalation, as the played out fantasy gives way to real violence.

One of the key points in telling a story about role-play is how well the script manages to grasp the nature of the game itself. In The Wild Hunt the audience initially identifies with Erik. Erik’s life is a mess: he has no job, no perspective and no real identity. He spends large parts of his free time caring for his deranged father, who does not even recognize him anymore. The only light in Erik’s life is his new girlfriend. On the other side we have his older brother Bjorn, whose life isn’t much better, but Bjorn found a way to deal with it. Bjorn is the prototypical escapist, who lives only for the next weekend when he leaves his real-life existence behind to become a mighty viking warrior wielding a hammer forged by the gods themselves. His father’s illness has caused Bjorn to lose interest in everything but the game, which his brother can not forgive him for. The third of the main characters is Lyn, Erik’s girlfriend. She mimics an archetype coined by the dark youth cultures of the 90s: The melancholy happy-go-lucky. Despite her dark goth-style appearance, when confronted with real tragedy, represented by Erik, she decides to run from it and flees into the same fantasy world as Bjorn. A world where she is able to keep her image of the inapproachable princess. She is also characterized by a childish naiveté in both social and sexual matters. It is in this naive escapism, that she of the three main characters most clearly reflects the essential desires addressed by role-playing.

The original conflict is based on these three characters. When Erik arrives at the playing grounds, he is not willing to participate in the game, as he is completely focused on his real-life problems. When he violently intrudes into the fantasy world, he is immediately recognized as an alien, which inhibits all of his attempts to reach Lyn. Only when he accepts the help of his brother, who is familiar with the rules of the game, he is finally able to achieve what he came for. However, with Lyn is removed from the game, the other players are unable to sustain the illusion. Out of this spawns the initial frustration, leading to the final violent escalation.

Most of this works remarkably well. Writer/Director Alexandre Franchi and his co-writer Mark Krupa, who also plays Bjorn, manage to nail their characters with magnificent precision and establish a believable setup for the bloody showdown. They also establish a colorful cast of supporting characters who illustrate the numerous factors contributing to the role-play experience: We have the two referees, again one with an escapist and one with a realistic mindset, the ambitious first-time player eagerly longing for great adventures, the strong female elven leader who uses the game to subversively act out sexuality, and many more. It is through these characters that the script truly manages to get a grasp on the fundamental fascination of role-playing: The ability to leave one’s real identity behind for a limited time and assume the self-determined role of an entirely different character.
While the script has a few flaws – the way the ‘evil’ players behave and eventually freak out seems a little undue; Lyn’s character lacks a clear arc; etc. – those are easily made up for by the numerous well-plotted characters.

A final note must be made on the amount of depicted violence. While most of the movie works as a social drama with frequent scenes of comic relief, the final showdown is dominated by scenes of shockingly brutal violence. Since the players have no weapons at their disposal – mind you, LARP is played with harmless rubber weapons – they fight like cavemen with sticks, stones and fists, which adds a creepily intensive realism. If you have a light stomach, you should strongly consider skipping this one.

Conclusion: Given that a movie dealing with this theme had to come out eventually, The Wild Hunt does its job incredibly well. The script manages to give a detailed insight into the mechanisms of role-play, without discrediting its players. If you have a general interest in the topic and don’t mind the graphic scenes near the end, do not hesitate to give it a try.

There is currently no known release date in Germany.

Categories: Movies

Movie Review: Two Eyes Staring

September 9th, 2010 No comments

Little Girl Gone Wild

Two Eyes Staring/Zwart Water
Directed By Elbert van Strien
Starring Hadewych Minis, Barry Atsma, Isabelle Stokkel
Two Eyes Staring at the imdb

Watched at the Fantasy Filmfest 2010 in Munich.

When Lisa’s grandmother, who she never met, dies, she and her parents move into her old house in the Netherlands. Lisa, upset because she had to leave her friends behind in Belgium, soon begins to tell stories of a little girl living in the basement who seems to have unfinished business with Lisa’s mother.
In many ways, Two Eyes Staring is probably best described as a blend between A Tale of Two Sisters (the US remake of which ran in cinemas last year) and Del Toro’s The Devil’s Backbone. The main focus of the movie lies on the ghost-story and it manages to tell this story well, however it feels a little too plain at times. Were it not for the surprising ending and a few well-executed scenes scattered in between, the movie would feel like a perfect example of by-the-book horror cinema.

It is therefore not surprising if you get a little bored during the 110 minutes of runtime. Too often you will get the feeling that you have seen all of this before – the three-character setup of the parents and the child; the slow escalation of events accompanied by an increasing prominence of the ghost in the house; the high-contrast low-color lighting scheme, which is so popular in European cinema these days. It is simply too much that is too well known.

Fortunately, the movie manages to throw in just enough bits of interesting plot elements to keep you from drifting away and the ending eventually makes up for most of the lengths encountered in between.

Conclusion: Fans of the genre will like this movie. It is well-crafted, avoids most common mistakes of the genre and in the end even manages to mix some really new ideas into an otherwise fairly standard plot. If the script was just a little more tight and the number of memorable scenes a little higher, this could have been a really good movie.
Since they are not, we will have to be satisfied with a decent piece of genre cinema. Which, mind you, is still more than most movies out there are able to achieve.

There is currently no known release date in Germany.

Categories: Movies

Movie Review: Tucker & Dale vs. Evil

September 5th, 2010 No comments

Backwoods Blowout

Tucker & Dale vs. Evil
Directed By Eli Craig
Starring Tyler Labine, Alan Tudyk, Katrina Bowden
Tucker & Dale vs Evil at the imdb

Watched at the Fantasy Filmfest 2010 in Munich.

Tucker and Dale are your average textbook hillbillies who just want to enjoy a quiet weekend of fishing and beer-drinking in the woods. Which they would probably get, weren’t it for the college kids inhabiting the hut across the lake, who mistake their rural neighbors for backwoods serial killers. When the two save one of the college girls from drowning in the lake at night, the juveniles believe themselves in a grade-A backwood-slasher and soon turn the movie into the very same by their futile attempts to fight the harmless hillbillies.

Of course this only works because the movie regularly switches perspective from the hillbillies harmless leisure activities to what the kids make of them. This results in a highly enjoyable succession of bizarre killings à la Final Destination, before the movie takes a turn for the more serious at about two thirds through.
The main reason this all works so well is the hilarious performances by Tyler Labine and (this author’s long-term favorite) Alan Tudyk, who obviously had a hell of a lot of fun with their roles as Dale and Tucker respectively. While Labine gets the bigger part and a full-fletched character arc, Tudyk does what he does best: Insanely silly mischief.
Without spoiling it can only be said, that the script leads them through parodies of nearly every characteristic moment of the backwoods-slasher genre, which left the audience in the sold-out theatre in Munich screaming with laughter.

And there really is not much more to say. The premise of the script is as simple as it is brilliant and even with the more serious plot developments towards the end of the movie, it remains as enjoyable as one could ever wish from a horror comedy.

Conclusion: Unless you have an unexplainable aversion towards smart horror-parodies, there is no reason not to like this movie. Preferably to be watched amongst a crowd of horror-aficionados who know the genre well enough to recognize the countless references and are not afraid to laugh themselves silly over some good old splatter-slapstick.

There is currently no known release date in Germany.

Categories: Movies