A blog about in-depth analysis and understanding of programming, with a focus on C++.

Tuesday, December 04, 2007

Interface my Code

Once upon a time, I talked about Pimpl. Basically, its a way of hiding the implementation details from external code. The primary benefit is improved compile times, since header files do not have to include a bunch of code that nobody cares about, and only the internals of a system need recompiling when external code changes.

As the primary example for Pimpl, I used what I described as "entity from a game system". The example brings up a specific problem, one that is not unique to games. That is, of the "fat interface" object, the object that needs to know about everything, everywhere.

Here is that example again, pre-Pimpl:

class GameEntity
{
public:
GameEntity();
virtual ~GameEntity();

private:
boost::shared_ptr<Render::MeshClass> m_Mesh;
boost::shared_ptr<Anim::AnimSystem> m_AnimationSystem;
std::vector<boost::shared_ptr<Audio::Sounds> > m_SoundList;
std::vector<boost::shared_ptr<Coll::CollisionMesh> > m_CollList;
boost::shared_ptr<Physics::PhysicalObject> m_PhysicsObj;
};


Now, the Pimpl version does a great job of hiding the internals:

class GameEntityImpl;

class GameEntity
{
public:
GameEntity();
virtual ~GameEntity();

private:
boost::shared_ptr m_Impl;
};


Now, the only #include that the external system needs to do is with boost/shared_ptr. That's all well and good, but there's something missing... the interface. GameEntity isn't terribly useful without its interface. And while we no longer expose numerous classes from the internals, the interface would force us to do so:

class GameEntityImpl;

class GameEntity
{
public:
GameEntity();
virtual ~GameEntity();

void EnumerateCollisionVolumes(boost::function<void(boost::shared_ptr<const Physics::CollisionVolume>)>) const;
void EnumerateMeshes(boost::function<void(boost::shared_ptr<const Render::Mesh>)>) const;
boost::shared_ptr<Ai::AiController> GetAI();
boost::shared_ptr<const Ai::AiController> GetAI() const;
void PlayAnimation(boost::shared_ptr<const Anim::Animation> pTheAnim);

void SetPosition(const Position &thePos);
void SetOrientation(const Orientation &theOrientation);

private:
boost::shared_ptr<GameEntityImpl> m_Impl;
};


This is really no better than before. Oh, it still gets rid of certain implementation details, like the use of std::vector and such. But that's all; the user still needs the definition of a lot of classes.

Also, there is the problem of having fat interfaces. What I depicted is the tip of the iceberg; it is not uncommon for the core entity classes to have hundreds of member functions.

There is a way to solve all of these problems at once. Basically, you use your Pimpl differently.

Pimpl has a publicly available interface class, but also a private implementation class. The solution is to, instead of having one public interface class, have many. One for different uses of the class.

So, in this case, GameEntity objects would be created through some global function interface. The code that creates a GameEntity needs to know about all the different bits, so you can't avoid that one. But what is created is the private implementation, which is stored by boost::shared_ptr in a list accessible by a unique name. It would also create the primary public interface class, called GameEntity, which is returned to the user.

GameEntity would have a number of functions that would return different interfaces. So, there would be a physics interface, rendering interface, sound interface, etc. These all expose functions and classes that much of the game code can freely ignore. All of them have shared_ptrs back to the private implementation.

So, your GameEntity looks like:

class GameEntityImpl;
class PhysicsEntity;
class RenderEntity;
class AiEntity;
class AnimEntity;

class GameEntity
{
public:
virtual ~GameEntity();

//Interface retrieval
boost::shared_ptr<PhysicsEntity> GetPhysicsEntity();
boost::shared_ptr<const PhysicsEntity> GetPhysicsEntity() const;
boost::shared_ptr<RenderEntity> GetRenderEntity();
boost::shared_ptr<const RenderEntity> GetRenderEntity() const;
boost::shared_ptr<AiEntity> GetAiEntity();
boost::shared_ptr<const AiEntity> GetAiEntity() const;
boost::shared_ptr<AnimEntity> GetAnimEntity();
boost::shared_ptr<const AnimEntity> GetAnimEntity() const;

std::string GetEntityName() const;

void SetPosition(const Position &thePos);
void SetOrientation(const Orientation &theOrientation);

private:
GameEntity();

boost::shared_ptr m_Impl;

This works perfectly. It hides everything from prying eyes, while creating interfaces for specific kinds of tasks. Because all of the interfaces point to the same object, they can call any internal code they wish. So the generic GameEntity interface can have expose functions like SetPosition which operate on the physics object (the ultimate source of the position) without having to go to the PhysicsEntity to do so. The PhysicsEntity will expose it of course, but it will also expose other functions.

Also, if the necessary objects for a certain kind of interface don't exist for a particular GameEntity, then the interface retrieval function will simply return NULL. It forces the user to check if the GameEntity, for example, has an AI before calling AI function.

I call this "Interface-based Programming". The data is perfectly hidden behind specific views into that data, with each view being categorized for specific functions and purposes.

Thursday, September 20, 2007

Parsing, C++ Style. Part 1

I'm going to let you in on a secret: I hate rote busywork programming. I can't stand boring, trivial code. Oh, I know it needs to be done, but if I've done it 20 times before, odds are I'm going to find a way to keep from having to do it a 21st time. That's one of the things that's nice about using C++.

One task I detest with a passion is writing a parser. We've all had to do it. Load a text file, tokenize it based on some pattern or whatever, look for the data you're interested in, check for errors in formatting, etc. It's easy and boring. And, more importantly, highly error prone.

The latter is a real sticking point. The only thing I hate more than writing a praser is having to debug one. Adding to that is the fact that debugging a parser is not like unit testing or something of that nature; it's a totally input driven process. If you forget a particular case, or didn't anticipate something, it will come back to bite you.

Fact is, I shouldn't have to parse code manually. I should have some API or library or something that I can apply to a text file and read it. Worst case, I should be able to define the format of the file, run the file through the parser with that format, and get a parse tree representation of the file, or an error if the file is not formatted properly. Best case, instead of a parse tree, my code simply gets called during the act of parsing the file in question.

Now, there are two utilities in particular that I personally rely upon for 95% or more of my parsing needs. The first is LibXML2. It's a C-based library (though there's an unpleasant C++ front end with an obnoxious license available, if you really need it), so it doesn't qualify as C++ Limit Science.

So, I lean on XML a lot. However, though XML is a nice language for the person writing the document, it's not particularly nice for the person reading it. Even with LibXML2, you have to do a lot of rote parsing work.

For formats that tend to be generated from code rather than by an external user, I prefer a combination of Lua and Luabind. Lua is a very easy to use scripting language, but the origin of Lua was in configuration languages. As such, Lua makes a handy file format. All you do is have the Lua script store its data as a large table, likely with nesting. Imagine a gigantic arbitrary struct with any data you can imagine. It can be passed around as plain text, but it can also be compiled into Lua bytecode for size purposes. And thanks to Luabind (a C++ library), actually accessing the contents of the object is much easier than with the Lua C API. It is very easy to generate files and it is likewise easy to read them. And if someone wrote the file in a syntactically erroneous way, Lua will simply report an error at compile time.

Taken together, they cover most of my parsing needs. XML is used for things the user will modify or create, and Lua is used for most everything else.

But then comes the big problem. What do you do when you have no control over the file format?

This is more likely than you might think at first glance. HTML and XML may be derived from the same source (SGML), but they are not compatible. And HTML certainly isn't Lua. Or maybe you're trying to write a scripting system yourself. Unless you're going to force your users to script in XML (yuck), you will need to write a parser of some kind. Or maybe you need to be able to parse some simple strings that the user enters, such as for a text adventure or some such.

And that's where Boost comes in. Or rather, Boost.Spirit.

Spirit is a very large, header-only library, but it's founded on a simple predicate: make text parsing easy. It is designed for low-to-medium scale parsing; high scale parsing, as for a highly grammatically complex language like C++, is at the very far end of Spirit's capabilities. It can do it, but you will certainly not like what it does to your compile times.

This is to contrast Spirit with it's primary alternatives, the preprocessing-based Lex/Yacc and Flex/Bison. These alternatives require the user to write a grammar in a special syntax and add specialized preprocessing commands into their build pipeline to generate C files for doing the parsing. They're functional, but rather arcane, requiring external executables and so forth.

Spirit, by contrast, leverages a preprocessor that you already have: your C++ template preprocessor. Spirit is a template metaprogramming library that allows the user to define a grammar using a syntax that is not unlike Enhanced Backus Naur Form. It also allows the user to provide semantic actions, functions that will be called when a certain parsed element is reached.

Higher-level Spirit functions, built on the base functionality described above, allow for the creation of an entire parse tree of the document, defined by special semantic actions. And of course, Spirit has the Phoenix functional programming library in it (which it uses internally), which makes it easier to build semantic actions for various bits of functionality.

The core foundation of Spirit isn't really a parser per se: it is a tokenizer and recognizer. It is, in essence, a schema language. It takes a string and a grammar and returns true if the string fits the grammar and false otherwise. Along the way, it will call various user-defined functions on the parsed text if it matches.

Here is a very simple example of a Spirit parser:
using boost::spirit;

bool RecognizeNumberSequence(const char *strInput)
{
return parse(strInput, real_p >> *(',' >> real_p), space_p).full;
}

This will return true if strInput is a comma-separated list of real numbers.

The second input to the function is a Spirit parser. The expression real_p >> *(',' >> real_p) produces a parser object that can be used as input to the parse function. The variable real_p is a predefined Spirit parser that parses real numbers in all of their various forms.

The object space_p is a parser recognizing spaces. Used as the third argument to boost::spirit::parse(), it represents the "skip parser". This parser tells the parse function how to tokenize the input. That is, what to ignore when parsing. This keeps the main grammar cleaner.

Let us break down the main parser expression, "real_p >> *(',' >> real_p)". The '>>' operator means that the operand on the left will occur before the one on the right. The '()' means what it seems like; the grouping operator. The '*' operator (C++'s dereference, not multiply) means that the operand will be repeated zero or more times.

So, this parser means that there will be a real number, followed by zero or more repetitions of a comma followed by a real number. Basically, a comma-separated list of real numbers. And the space_p skip parser means that spaces can occur between any of these elements, but not within a real number.

As mentioned, this is not a terribly useful function, as it does not actually do anything with what it parses. It simply detects whether the input is well-formed as stipulated by the two parsers. In the next part, we will deal with actually doing something while parsing.

Cast in Gold

Casting. Casts are that dirty little thing that everyone knows not to use but everyone uses it more often than ought to.

C introduced us to one kind of cast. You have a thing of type A, and you can try to turn it into a thing of type B. Simple construct for a simple language.

C++ created more casts. But there's a funny thing about C++ casts; nobody likes them. Articles have been written about why they should be avoided and why they were a bad idea for the language. Few C++ programmers prefer them over C-style casts.

This article will explain what these C++-style casts are and why you should use them a lot more often that you probably do.

All of these casts share a common syntax. It reads like, cast_name<TypeToConvertTo>(expressionToCast); No matter how small "cast_name" is, it will always be bigger than the nothing that C-style casts use. C++-style casts draw attention to themselves by being large and ugly.

This was supposedly done intensionally as a way to discourage their use. Unfortunately, it worked; that's why most everybody just uses C-style casts. The idea was that it would discourage the use of casting in general, but that was madness.

There are 4 kinds of C++ casts. There is the static_cast, const_cast, reinterpret_cast, and dynamic_cast. Each of them is meant to be used under specific circumstances.

The easiest two to defend are reinterpret and dynamic. This is because neither of them have a direct analog with C-style casts.

If you have a 32-bit float value, and you want to get it's bit-pattern as an unsigned 32-bit integer, in C, you have to do this:
unsigned int32_t iFloatBits = *(unsigned int32_t *)&fValue;

Not only is this somewhat obtuse as to what's happening (you're pretending a float-pointer is an integer pointer), it requires that fValue have a memory address, since you have to dereference it. This is highly unnecessary; what you're really asking is for the compiler to bring fValue's bits from a float register into an int register. This is very simple and just about every CPU can do it. But in C, you can't actually say it like that.

In C++, you have:
unsigned int32_t iFloatBits = reinterpret_cast<unsigned int32_t>(fValue);

The only downside is the sheer length of the expression. But it's a lot more obvious what's going on, so long as the reader understands what C++ casts do.

As for dynamic_cast, there is no C analog because C doesn't have inheritance. The purpose of this operation is to take a pointer to a base class and turn it into a derived class. Now, of course, that particular base class instance may not actually be that particular derived class. So a regular C-style cast operation is right out, since they are compile-time operations.

See, dynamic_cast isn't a cast at all really; it's a function call. One that can fail. That is, a particular cast can return NULL if the cast operation cannot work on the specific instance. A dynamic cast is what allows some degree of runtime-type identification. You use it when you need to break the OOP contract and consider a base class to be a derived class.

So, it is obvious that both of these are superior to their C analogs (and one doesn't even have a C analog, so it is a priori superior to nothing). However, what of the other two?

Well, the main advantage of using const_cast is just error checking. A const cast can only add or remove const-ness from a type. As such, you cannot accidentally convert the type to something else by entering the wrong type. This is useful.

Also, removing const-ness (which is 99.9% of all const_cast uses, since adding const-ness is implicit) is rather rare. It usually happens when interfacing with code that you did not write. Indeed, doing this kind of cast suggests a design flaw or that something ought to be made mutable. Outside of that, the times when this kind of cast is truly needed are few indeed. So the larger size of the conversion expression is not a substantial problem.

Which brings us to static_cast. This is the cast that I can recommend the least. It gives protection from accidentally doing something that reinterpret or const casts should be used for, but that's about it. And so long as we assume that the programmer is intelligent, and therefore is not going to arbitrarily be doing these kinds of conversions, then we should assume that each static cast operation is actually necessary. And if the programmer actually is not intelligent, the ugliness of static_cast will not be a deterrent, since they can always use C-style casts anyway.

So, either way, it generally better to use C-style casts when you might use a static_cast.

Wednesday, September 12, 2007

One RAII to Rule Them All

RAII: Resource Acquisition Is Initialization.

Perhaps the most useless acronym ever.

Why? Because it only tells you half of what it is. Perhaps even less.

The part of RAII that is in the name is the concept of wrapping all resources in objects. Specifically, the constructor of objects.

What the name misses is the basic construct of RAII.

Consider the following code:
char *LoadAndProcessData(const char *strFilename)
{
FILE *pFile = fopen(strFilename, "rb");
if(!pFile)
return NULL;

char *pFileData = new char[50];

fread(pFileData, 50, pFile);
fclose(pFile);

ProcessData(pFileData);

return pFileData;
}

The points of failure in that code are legion. 'fopen' could fail, returning NULL. 'fread' could fail, returning NULL. Even 'new' could fail, either returning NULL or doing everybody's favorite: throwing an exception.

The first point, the function handles. Presumably, the calling function will look at the return value, check for NULL, and if it sees NULL, then it will do something useful.

The second point, however, gives rise to an unhandled error. If 'fread' fails, the function 'ProcessData' will get uninitialized (or partially initialized) memory. It may fail, throw an exception, or silently succeed but have bad data. None of these are good answers.

If 'new' fails, throwing an exception means that we fail. Granted, 'new' will only throw if we are actually out of memory, which isn't a terribly recoverable error. But if it does throw, we will immediately leave the function, without calling 'fclose'. That's bad.

File handles from a process tend to be cleaned up OK by the OS, mainly because so many programs are bad about doing it themselves. But it could have been a global resource, like a piece of memory to be shared between two processes (say, the clipboard). Throwing an exception would keep us from properly filling that memory out. Or closing the handle correctly, resulting in a leak. And so on.

It is precisely here where C-in-C++ programmers give up on exceptions entirely. After all, if 'new' didn't throw, we wouldn't have a problem.

However, there is a much more useful solution to this, one that doesn't turn its back on useful language features. The reasoning behind wanting to preserve exceptions has been discussed. What we will focus on is the technique that allows us to avoid abandoning exceptions. As well as giving us so much in return.

The reason that the acronym RAII is wrongheaded is that it isn't really the resource acquisition that's the issue; it's controlling when the resource is released. After all, 'fopen' does it's job; it creates a file handle well enough. The problem has always been that 'fclose' isn't guaranteed to be called.

The C++ specification dictates that, when an exception is caught, the stack will be unwound. All stack variables will have their destructor called, and it will be called in the proper order. Since this coincides with the exact moment we want to destroy the file handle, it makes sense to put the file handle on the stack.

RAII principles are as follows:
  1. Constructors are required to do actual work. Any and all things that could be deemed resource acquisition will only be done in object constructors.
  2. Any and all resource releasing will be done in object destructors.
  3. RAII-style objects must have appropriate copy constructors and copy assignment operators, if the object is copyable. If it is not, then it should forcibly be made non-copyable (by deriving from boost::non_copyable, for example).
  4. If there is a resource that cannot be created directly by a constructor, it must be created and immediately stored in an object with a destructor that will release the resource properly.
A few simple rules create a fundamentally different coding style.

What qualifies as "resource acquisition"? At a bare minimum, a resource is something you need to have cleaned up. Hence the backwards acronym; we define resources by wanting to get rid of them, not by how we acquire them.

Immediately, it becomes obvious that the value returned by operator 'new' qualifies. After all, memory is most definitely something we need to have cleaned up. So as per rule #4, the results of 'new' under RAII programming style must be store din something that will guaranteably call 'delete' when it is finished. We have already seen such an object: boost::shared_ptr. Shared pointers are reference counted, which doesn't qualify in terms of the guarantee, since circular references will prevent the resource from being collected. But they're close enough. There are also std::auto_ptr's and a few others.

So dynamically allocated memory is a resource. Files are resources. The GUI system windows, drawing tools, etc, are resources. And so on.

However, RAII is actually quite viral. Unless your entire codebase operates under RAII principles (or the non-RAII portions exist in isolated chunks at the bottom of the stack. Leaf-code), exceptions cannot be entirely safe. For our simple example of file openning, we can easily make it perfectly safe by creating a File object that acts as a RAII wrapper over the C FILE handle. The constructor opens the file, throwing an exception on failure, and the destructor closes it. For the simple case, it works just fine.

However, if you use that object in a non-RAII fashion (allocate it dynamically and store a pointer on the stack), you lose the guarantee. Which means that your entire codebase must hold everything allocated in a RAII fashion to be perfectly safe in throwing exceptions.

There is an implicit 5th rule as well. Because constructors are the only place where resource acquisition is to happen, they also must throw exceptions if the acquisition fails. That is the only way to signal the non-creation of an object. Generally, releasing a resource can't provoke a failure, so it is not much of an issue.

This is generally where the C-in-C++ programmers who stuck around decide to leave, as it requires a new coding style.

What gets lost are the benefits. After all, if you're being forced to use shared_ptr everywhere, memory leaks will be a lot less likely. Circular references may happen, but that is what weak_ptr is for. Your code will generally be very difficult to break from a resource perspective.

Also, exceptions are a very useful piece of functionality. They can be used as a complex piece of flow control (but only sparingly, and only when it really matters). And, as mentioned earlier, they have a vital role in reporting certain kinds of errors.

In general, it is a more rigid programming style, requiring, particularly for shared_ptr relationships, a degree of forethought in the design phase. But it can save a lot of time dealing with errors, memory leaks, and other concerns.

Sunday, July 22, 2007

Errors, Exceptions, and Limit Science

As it currently stands, there are basically two kinds of C++ programmers. The distinctions can be more subtle than those I'm about to describe, but it works well enough for these purposes.

First, there are the C++ programmers who pretend that C++ is really just C with classes. They're wary of any form of templates, RAII, and so forth. And exception handling is right out for these people. They solve problems as a programmer would with C, though they may dabble in some C++-isms. The STL is looked on with a wary eye, if for no other reason than that it does not do things the C way.

The other kind of C++ programmer are those more like myself. They see C++ as a fundamentally different beast from C. They solve problems in C++ by using C++ wherever reasonable. They tend to avoid C-isms like function pointers. Some of these people overindulge in certain aspects of C++, but in general, they're trying to use the language as it is meant to be used rather than merely taking the most obviously useful features and ignoring the rest.

And nowhere is the difference more clear than in the way that they deal with erroneous conditions. The C-in-C++ style programmer has only two real tools to handle error conditions: call exit() or some other form of immediate program termination (asserts, etc), or return an error code and let the user handle it.

Quick side note. The reason the C-in-C++ programmer refuses to use exceptions tends to be because they abhor RAII. You can't really use one without the other; you can't feel safe about letting exceptions unwind the stack unless you can guarantee that everything will get cleaned up. And you can't do that without embracing RAII fully. And then, if you're not throwing exceptions, you can't really use RAII, because exceptions are the only way to signal a constructor error. Thus it is a vicious circle; one must have either both or neither. "Do, or do not; there is no try."

In any case, the programmer who embraces C++ has more than just two options. Which is fortunate, as there are multiple different kinds of errors, and many C-style C++ doesn't recognize.

The first kind of error is the most obvious. Some piece of code has detected a condition so horrific that it feels that the program simply cannot continue. It must die, and do so immediately. It does not consult other code, and nobody has any way of preventing this. C handles this through exit(), but the C++ way is through std::terminate. In either case, they both mean the same thing: the program must die right now.

Of course, C++ didn't add std::terminate just because. You can actually set up a generic termination function that will be called by std::terminate. Now, this code really ought not throw any exceptions (or do things like allocate memory that might throw), because that's really bad inside of terminate. But outside of that, everything else is pretty much OK.

These kinds of errors are the kind you don't expect to get, but which may happen in the course of development. Asserts are another common way of dealing with them.

Next up is an error that is considered recoverable by the code that detects the error. What constitutes "recoverable" differs even from person to person (personally, I think throwing in the event of an out-of-memory error is absolutely wrong. The program should die immediately if not sooner). But in general, it is something that the code thinks that the calling code should be informed of.

C-style C++ has only one way of doing this: an error code. A return value from a function that the user code then detects and acts on.

There are basically two types of error codes: the kind that the calling code is supposed to ignore and the kind that it isn't. This corresponds to two basic subtypes of the recoverable error: "recover or die" and "recover or live".

What I mean by "recover or die" is that the calling code should either detect the error or the program's execution should halt. That is, the error, while being recoverable, requires action on the user's part. The error cannot be recovered from by pretending it didn't happen; someone external to the code must do something.

"Recover or live" is the opposite. It's OK if the user does not detect that the error happened. Everything is considered in a good enough state that the program can continue operating as normal. If the programmer wants to detect the error and do something, fine, but the program can continue as normal.

One big question a programmer has to decide when detecting a recoverable error is which kind it is. If you're writing a file, is it a recover-or-die or recover-or-live error? Technically, it could be the latter, but would a programmer appreciate that?

In any case, C-style C++ doesn't give the programmer the ability to handle "recover or die". After all, you can completely ignore any return value you wish, so error codes are meaningful only if the programmer using the code chooses to make them meaningful. The lower-level programmer has no way of forcing the higher-level code to handle the error.

In C++, exceptions provide a way to require testing the error code. However, there's another interesting way to force error testing that doesn't require some of the verbosity of exceptions. Exceptions allow any code in the callstack to handle the exception. The alternative method must be handled immediately at the call site. However, it does force the user to handle it, unlike error codes.

C-style C++ users do use this quite a bit themselves. Many functions that return a meaningful value (say, GetObjectType(x)) will return NULL in the event of a recoverable error. It is not a "recover-or-live" error, because the program will eventually reference NULL, thus causing death. But of course, this doesn't work on non-pointer types.

Thanks once again to the Boost library, we have Boost.Optional. Basically, a boost::optional<typename> object has either a (presumably valid) value of the typename or nothing. In order to use the optional value, the user must call a get function, which will assert in debug builds and do other unpleasant things in release builds. In either case, the user of the code is forced to at least think about the fact that the function may return nothing, since they have to store the boost::optional return value.

It is a weaker form than exceptions, but it is better and much less verbose in many cases. Particularly so if it is semantically OK for it to return nothing.

Real C++ style still can use errorcodes for "recover or live" errors. But it is good that C++ has ways of forcing the user to handle errors or the program will crash.

Thursday, January 11, 2007

Functional Programming in C++ 3

I'm skipping part 2, because I'm having some difficulty coming up with a really good example of why you need functional programming. Oh, sometimes it's nice, but I've yet to see a point where it's necessary.

We've actually had a slight introduction to functional programming, or some aspects of it before. The article involving Boost::Bind, for example. At the end, I mention the ability to compose bound functions to create new ones. And Boost::Bind has the ability to convert the parameters of a functor from one to another.

Of course, that's not nearly good enough for real functional programming.

In terms of getting this done in C++, there are several options. Boost (the fount from which all good things flow), of course, has a library called Lambda. This allows you to generate functions of the form:
cout << (1 + _1)

This creates a function object inline. This function object takes one parameter, adds one to it, and prints it to standard out.

However, Boost::Lambda has a number of limitations. Chief among them, having only 3 arguments, which can be quite limiting. Also, it replicates Boost::Bind (and is incompatible with Boost::Bind), but replicates it without the powerful Boost:MemFn functionality, so you can't use member functions in Boost::Lambda.

In short, Boost::Lambda doesn't take things to the limit, doesn't give us all of functional programming. So, how do we do it?

Within Boost lies the greatness that is Spirit. Spirit is a parser framework; you build parsers with it in a style that seems very similar to Extended Backus–Naur Form. But that's not why we're talking about Spirit. For within the Spirit slumbers a beast of great power: Spirit::Phoenix.

Phoenix is functional programming. It is Boost::Bind combined with Boost::Lambda, mixed with stuff that you never expected to see in C++. Behold:

for_each(c.begin(), c.end(),
if_(arg1 % 2 == 1)
[
cout << arg1 << ' '
]
);

Yes. That does exactly what it looks like. Yes, that is an 'if' statement. Yes, that 'if' statement is in a place where no 'if' statement ever belongs. And yes, that entire expression generates a function object that is passed to for_each and does what it says it does.

So, let's take things slow.

Remember back to our discussion of Boost::Bind. It used entities named '_1', '_2', etc to represent the first argument, second argument, etc. Phoenix uses 'arg1', 'arg2', etc. Personally, I prefer Phoenix's style, since it uses something close to a word, and I'm pretty wordy in my naming.

Let's start with a basic example:
cout << arg1;

This statement does not execute 'cout <<'. It merely creates a functional object of one parameter that outputs that parameter.

Note that the functional object created does not know nor care what the type of that object is. As long as the operator << has been overloaded for whatever argument you pass in (and that the overload takes the appropriate type, in this case an output stream object), then you may call this function.

This goes back to our discussion in part 1, where we said that a function 'f(x) = log(x)' could only take positive values of X or 0, because the function log(x) is undefined on negative numbers. This is the same thing.

Note that there is one thing we cannot do, just as we couldn't do with Boost::Bind: catch this function and store it directly. Oh, 'for_each' can do it, but that's only because 'for_each' is templated on the type of that function object. It can store it because it doesn't need to type out what that type is.

The type of 'cout << arg1' is not for the feint of heart. And that doesn't even get into some of the functional programming statement stuff.

Then again:

boost::function<void (int)> integerOutputer = cout << arg1;
integerOutputer(5);

Maybe we can ;)

Thanks to Boost::Function, we can capture one of these lazy functions and store it. The only limitation is that, because C++ is strongly typed, we can only capture it in a strongly typed object. So we loose a lot of the type genericness that we would normally have with lazy functions.

A minor limitation, but one we can accept. Especially since most of the time you're wanting to capture it (for say, a registered callback), you can easily tie down a specific argument list.

Another thing we can do is get local variables involved:

int iSomeValue;
for_each(someVector.begin(), someVector.end(), cout << (variable(iSomeValue) + arg1));

This will create a reference to iSomeValue, store it in the functional object, and use it on each envocation. If you have a list, and wish to take the summation of that list:

int iSomeValue;
for_each(someVector.begin(), someVector.end(), variable(iSomeValue) += arg1);

Much easier, and much more intuitive, than making a functor call.

It's important to note that 'variable(iSomeValue)' itself is, much like 'arg1', a functional object. So, you could execute, 'variable(iSomeValue)()', if you like really convoluted ways of getting the value of something.

Though I haven't discussed Boost::Signal, it is one of the chief places where you're going to want to use functional programming, aside from std::algorithms. There is a big difference, though: std::algorithms will immediately use your functional object and then destroy it. Signals, pretty much by definition, will call it whenever it feels like.

That's bad when you use 'variable(iSomeValue)'. That's because this variable will not exist by the time the Signal calls it. Remember: the variable() function stores a reference to the variable. And likewise remember that references to stack variables can be hugely dangerous if someone stores them and uses them outside of your scope.

Just FYI.

To get a bit more formal about things, what 'cout << arg1' is, in Phoenix parlance, is a "lazy function". This means that it is only evaluated when it's arguments are called. However, it is important to note that anything that is a Phoenix lazy function can also have certain things done to it. Like this:
(cout << arg1)(arg2, arg1)

This creates a new lazy function. This new lazy function takes two arguments. But the original one only took one.

Fortunately, Phoenix doesn't care. What this will do is create a lazy function can can only be evaluated to produce an answer if it is given two (or more) parameters. Because of how this function was constructed, the first argument will be discarded, and the second will be sent to standard out.

This becomes very interesting when you start using non-lazy functions as lazy function. AKA, when you start doing Boost::Bind-like stuff:
bind(&SomeFunc)

This returns a lazy function. It takes the arguments that the function, functor, or member function took. However, that may not be what you want, especially if that is a member function.

Just like with Boost::Bind, the first argument to a member function is the 'this' pointer. If 'this' isn't going to be an argument you expect the caller to use, you need to add it yourself:
bind(&TheClass::SomeFunc2)(*this, arg1, arg2)

Notice that it takes a reference to the class rather than a pointer. This means that using a smart pointer is impossible, so you need to make sure that your code makes it impossible for this code to be called beyond the lifetime of this object. Fortunately, Phoenix version 2 will allow the use of references, pointers, or smart pointers.

Because Phoenix is a lot more powerful than Boost::Bind, it can do some of the things that we only wished Bind could do:

bind(&TheClass::SomeFunc2)(*this, arg1, arg2 + bind(&TheClass::SomeFunc3)(*this, arg3));

Which creates a functor that takes 3 arguments and does functional composition. Even modifying the return value of the call to SomeFunc3 before passing it to SomeFunc2. You can do all kinds of nifty stuff with this.

The farthest limit of Phoenix however, comes from so-called 'lazy statements'. I scared you with this at the beginning:

for_each(c.begin(), c.end(),
if_(arg1 % 2 == 1)
[
cout << arg1 << ' '
]
);

The 'if_' construct is a lazy statement that evaluates it's contents (within the square brackets) only if the condition is true. Just like a regular 'if' statement.

Except that, since it's functional programming, it creates a function object that does this. And 'if_' is just the tip of the iceberg. Just about every C++ flow control construct exists as a lazy statement (except goto. But we don't use that anyway). Blocks of code can be multi-line, through the use of comma-separated elements:

if_(arg1 % 2 == 1)
[
cout << arg1 << ' ',
cout << (arg1 + 5) << endl //Note: no comma on the last line.
]

This is incredibly powerful.

However, it's important not to go thinking you can just write anything as a lazy statement. For performance reasons (or potential ones; these constructs have not been deeply profiled), it's generally a good idea to use lazy statements for rapid development, then factor them out into real functions that get called. The thing I like about them is that you can use std::for_each() and make it look like a regular for() statement.

This article is getting long, so I'll punt on closures. They're not the most important or interesting part of Phoenix anyway.

In any case, what you've seen here is merely the beginning. Through Boost::Function and Phoenix, you can have serious functional programming. You can create unnamed functions, compose functions, pull functions from standard C++ functions, and even build functions in a pseudo-C++ language.