Have Your Efficiency, and Flexibility Too

Metaprogramming Techniques For No-Compromise Code

by Nick Sabalausky

Full source code for this article is available on GitHub, or can be downloaded here.

View this article on a Single Page or Multiple Pages

Table of Contents:

  1. Have Your Efficiency, and Flexibility Too
  2. First Attempt: Send Efficiency and Flexibility to Dr. Oop's Couples Therapy
  3. Respecting the Classics: Old-School Handcrafting
  4. Success at Dr. Metaprogramming's Clinic
  5. It Walks Like a Duck and Quacks Like a Duck...Kill It!
  6. Metaprogramming Plus: The Flexibility Enhancements
  7. The Last Remaining Elephant In The Room: Runtime Conversion
  8. Curtain Call

Curtain Call

Although the examples throughout this article have focused on situations with large numbers of simplistic objects, these techniques can also work very well with smaller numbers of objects that do a lot of processing. The templated versions of UltraGiz give just a small glimpse of this.

We've seen that you can use metaprogramming with structs to achieve much of the same flexibility as classes while avoiding the class overhead. But even if you do go with classes, these metaprogramming techniques can still aid in optimization without forcing you to cut features and give up flexibility.

Ok, so towards the end we did start running into more tension between efficiency and flexibility. Perhaps those wacky nuts will never fully resolve their differences. But even so, they've made some major progress.

Whenever you have values or settings that don't need to change, you don't have to choose between eliminating them for efficiency and keeping them for flexibility. You can have both, with no compromise. And even if your values and settings do need to change, but just not constantly, you still have many options available for getting your efficiency and flexibility to cohabitate peacefully. So go ahead, use metaprogramming to have your efficiency, and flexibility too.

Thanks to Lars T. Kyllingstad, bearophile and Timon Gehr for their suggestions.

Table of Contents:

  1. Have Your Efficiency, and Flexibility Too
  2. First Attempt: Send Efficiency and Flexibility to Dr. Oop's Couples Therapy
  3. Respecting the Classics: Old-School Handcrafting
  4. Success at Dr. Metaprogramming's Clinic
  5. It Walks Like a Duck and Quacks Like a Duck...Kill It!
  6. Metaprogramming Plus: The Flexibility Enhancements
  7. The Last Remaining Elephant In The Room: Runtime Conversion
  8. Curtain Call