On the richness of C++

Walter Bright newshound1 at digitalmars.com
Tue Apr 15 13:28:35 PDT 2008


Edward Diener wrote:
> Walter Bright wrote:
>> 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.

C++0x has a "constexpr" proposal, which is a small subset of D's compile 
time function execution capability. For example, constexpr cannot do 
recursive functions, loops, if statements, etc.

> 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 used in std.algorithms for fun things like specifying the shape of 
the ordering function used in a sort.


> 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.

Yes, it can access the local variables from where it is instantiated. 
(What it does is expand the template into a nested function.) The 
template designer doesn't need to know, it's just another aliased 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.




More information about the Digitalmars-d mailing list