Tagged templates

So, I finally found some time for a Template Metaranting follow-up post. This time let's get down to business as this one contains a fair amount of code.

Sadly, I won't rant as much but instead I'll try to show how awesome D's templates really are. We'll write a piece of code, based on this Scheme implementation, that is, a simple monad) that we'll use to build a binary tree, with uniquely numbered nodes containing their height, without any global state (therefore purely) entirely at compile time.

Quick Reader, grab my code!

ADVENTURE!

Continue reading

Here's another post in the series, this time it's C++ vs. the D programming language.

Let's talk about templates. If you've ever tried templates in C++ you surely as hell recall the pages and PAGES of compiler errors and seemingly random placement of typename keyword. Trust me, there are EVEN WORSE problems with templates in C++... Consider the following:

struct Foo {
  template<int N>
  void bar() {}

  template<int N>
  struct Bar {};
};

template<typename T>
void f() {
  T foo;
  foo.template bar<0>();         // Line 12
  typename T::template Bar<0> b; // Line 13
}

int main() {
  f<Foo>();
}

Continue reading