I think that the source of many problems is doing something at the wrong moment. C++ is quite flexible, but a lot of its features can be accessed only in very awkward way. For example there is a limited support of compile-time evaluation, but accessible only with abusive use of templates*. There is no built-in reflection/serialization system — but again, people use templates and other language features to simulate it. Given enough macrodefinitions (and possibly Boost), everything is possible…

But this is so wrong. The key to success is using right tools at the right time. Just few ideas:

  1. If doing X in some language/tool is difficult, outsource it. Maybe other languages/tools are more suitable to do it.
  2. If you can isolate X in separate module/application, do it. Unix is all about small task-oriented application communicating over simple interfaces — and after 40 years it’s still doing great!
  3. Pre-processing and post-processing is not only about lighting and shading. They can be applied to any kind of data, including code.
  4. Auto-generated code is cool (as long as you control its generation).
  5. Have flexible build system. Be able to plug any kind of processing into it.
  6. Keep things simple!

And now, few stories and one longer example. When I started building my engine/pipeline in early 2009 I had many questions to be answered. One of them was game [engine] and editor communication. For some reason I decided to use C++ for all game code but C# for the editor**. I needed communication between those two — that includes sending some objects in both directions. C# features rich reflection/serialization support, but C++ is far more inferior about those. However, instead of using some template/#define trickery, I just made a simple application that generated both C++ and C# classes with all serialization needed from simple classes description in JSON-like language. Generated C++ code was extremely simple — no templates/macros/virtuals. Can I call it a win?

Once I was working on (non-gamedev) application that was using REST-like web services. How can you access web methods in your app? Some languages/frameworks (like C#/.NET) feature built-in support for those. For other toolsets the best solution could be similar to .NET — have a generator that creates code for all requests. And having custom generator is neat, because you’re not limited to one specific data/services descriptions (like WSDL/SOAP for .NET***).

Finally, live example. But first let me present a disclaimer: this is not a post about implementing properties in C++. Implementation is rough and may make your HDD and/or eyeballs explode. Thank you.

So it’s about properties. I mean C#-style properties. Imagine such code:

class Square
 
  {
 
  private:
 
    float _edge;
 
  public:
 
    @property float edge
 
    {
 
      get
 
      {
 
        return _edge;
 
      }
 
      set
 
      {
 
        _edge = value;
 
      }
 
    }
 
  
 
    @property float area
 
    {
 
      get
 
      {
 
        return _edge * _edge;
 
      }
 
      set
 
      {
 
        _edge = sqrtf(value);
 
      }
 
    }
 
  };
 
  void print(float edge, float area)
 
  {
 
    printf("edge = %f, area = %f\n", edge, area);
 
  }
 
  int main()
 
  {
 
    Square s;
 
    s.area = 25;
 
    print(s.edge, s.area);
 
    s.edge = 3;
 
    print(s.edge, s.area);
 
    return 0;
 
  }

to be valid C++. Well, using code pre-processing it’s possible.

Using this program: Metalua seems to be quite cool!).

Other files: makefile.

* C++0x constexpr, metaprogramming being fun, whatever. This is not a post about C++ pros and cons. Keep the big picture.

** Now I know that it probably wasn’t the best decision. But writing 2009 code with 2011 mindset would be too great.

*** Disclaimer: I’m no .NET expert.