On the richness of C++

Don Clugston nospam at nospam.com
Tue Apr 15 13:39:47 PDT 2008


Edward Diener Wrote:

> Walter Bright wrote:
> > Edward Diener wrote:
> >> Thanks for the examples above. I do realize that D has some built-in 
> >> features which enable template manipulation which Boost MPL has to 
> >> emulate through some very clever C++ template metaprogramming. In 
> >> particular, as you have shown above, D has the tuple concept, the 
> >> static if...else, and also something I like quite a bit, which is that 
> >> there is no necessity for a base template upon which specializations 
> >> are built but that each template can be defined with its own 
> >> specializations. That last feature makes D templates really much 
> >> easier to write.
> > 
> > One aspect of C++ templates is rendered completely irrelevant in D is 
> > D's ability to do compile time function execution. Any C++ 
> > metaprogramming that computes a value can be replaced in D with an 
> > ordinary function, that is then executed at compile time.
> > 
> > Other things that help are D's ability to pass strings as template 
> > arguments, and parse those strings at compile time, and D's ability to 
> > access local variables (not just globals) from template expansions.
> 
> 
> I assume the first means function execution which produces a constant 
> expression. I believe C++ has that on the plate for C++0x, but of course 
> you have evidently have it now.

Even if C++ gets it, it'll never be as good. The real fun comes when the compile-time function returns or manipulates a string. I don't think it's possible to do that unless the language has built-in strings and string manipulation.

> 
> I never realized that a string, by which I believe you mean a string 
> literal, could not be passed as a template argument which is a value and 
> not a type. It does seem an arcane area.

It's fundamental. AFAIK, C++ gains its Turing-completeness purely from the ability to have integral value template parameters. That's horribly clumsy. With a string, you can encode _anything_.
Consequently, instead of that horrible mess you get with expression templates, where an expression is encoded as a tree of types, you can simply encode it as a string. (Particularly nice for encoding a regexp!).

> The last point I do not follow. Surely a template can access its own 
> member variables, but you seem to be saying that it should access local 
> variables also where it is being instantiated. I can't imagine the usage 
> for that since the template creator can not possibly know where his 
> template is being used.

The name of the local variable can be passed as a template parameter.

> 
> Nonetheless, I will look at these features in order to to understand 
> them better. Templates are fun, and I imagine that D templates will be 
> more fun than C++ templates because they are richer and easier to use.

Indeed they are. Extremely addictive.




More information about the Digitalmars-d mailing list