On the richness of C++

Edward Diener eddielee_no_spam_here at tropicsoft.com
Mon Apr 14 04:31:50 PDT 2008


Bill Baxter wrote:
> Bill Baxter wrote:
>> Edward Diener wrote:
>>> Georg Wrede wrote:
>>>> Edward Diener wrote:
>>>>> Walter Bright wrote:
>>>>>
>>>>>> Edward Diener wrote:
>>>>>>
>>>>>>> Walter Bright wrote:
>>>>>>>
>>>>>>>> Kevin Bealer wrote:
>>>>>>>>
>>>>>>>>> I was amazed that Boost could do things like the Lambda support 
>>>>>>>>> with _1 _2, etc.
>>>>>>>>> Those Boost guys are geniuses.
>>>>>>>>
>>>>>>>>
>>>>>>>> I agree, they are geniuses. But that's really what is wrong with 
>>>>>>>> C++, you shouldn't have to be a genius to get advanced things done.
>>>>>>>
>>>>>>>
>>>>>>> Are D's templates a complete replacement in functionality for 
>>>>>>> Boost's MPL ? If so could you write an article on your web site 
>>>>>>> about the how's and why's of that ? I am still trying to 
>>>>>>> understand D's templates based on the sparse documentation of them.
>>>>>>
>>>>>>
>>>>>> I don't really understand Boost MPL, but D's template system is 
>>>>>> considerably more powerful than C++'s.
>>>>>
>>>>>
>>>>> I do not know the MPL either although I understand the general 
>>>>> concept. It is a C++ template metaprogramming library for 
>>>>> manipulating types at compile time so that the final result of the 
>>>>> body of a template deals in whatever types the template 
>>>>> metaprogramming needs to generalize for his programming task.
>>>>>
>>>>> I agree that what I currently understand of D templates looks 
>>>>> clearer than the template system in C++ but I am not knowledgable 
>>>>> enough to know whether it is "better", or easier to use in doing 
>>>>> the sorts of things which Boost programmers accomplish.
>>>>>
>>>>>> I agree that more documentation is needed, but one can easily 
>>>>>> write a book about it.
>>>>>
>>>>>
>>>>> The question is: has anyone tackled in D some of the template 
>>>>> metaprogramming tasks which various Boost programmers have 
>>>>> accomplished with C++ ?
>>>>>
>>>>> I am trying to get a feel for how different, or how much easier ( 
>>>>> or perhaps harder ) it would be to do Boost things like Spirit ( 
>>>>> lex/yacc-like DSEL ), function ( universal callable ), bind and/or 
>>>>> lambda ( function object creation ), shared_ptr ( sharable smart 
>>>>> pointer, obviously for RAII in D because of GC ), signals ( 
>>>>> generalized multicast events ), multi_index ( multiple index 
>>>>> containers ), regex and/or xpressive ( regular expressions ), 
>>>>> tokenizer ( generalized tokening of strings ), date_time ( 
>>>>> date/time and time intervals ) and many others ( the above are just 
>>>>> my favorites but I have hardly explored/used all of them ) which 
>>>>> C++ programmers find very useful. All of these libraries depend on 
>>>>> template metaprogramming in C++. Can their equivalents just as 
>>>>> easily be implemented and have any of them been done already ?
>>>>
>>>> Now the above is approaching the idea of this thread.
>>>
>>> In Boost there is an entire metaprogramming paradigm based on a set 
>>> of conventions regarding C++ templates and types, as implemented by 
>>> the Boost MPL. This is despite the fact that C++ template syntax is 
>>> much more abstruse than D's templates. So I was merely wondering if 
>>> there is anything equivalent in D which can manipulate templates the 
>>> way that the Boost MPL can. I agree that D's templates appear much 
>>> easier to use but whether or not they are richer in functionality 
>>> than C++ templates is something I do not know, but would love to find 
>>> out about. But that would mean a comparison between what D can do 
>>> with its templates as opposed to what the Boost MPL can do.
>>>
>>>>
>>>>> I have not looked at the D libraries, phobos and tango I believe 
>>>>> they are called, so maybe I am way off base comparing the Boost 
>>>>> libraries to what may already be in D. But I am trying to get an 
>>>>> idea if D is capable of doing these Boost things just as easily or 
>>>>> easier.
>>>>
>>>> Yes! And I think those are things that very may, who don't regularly 
>>>> write in this NG, really wonder about with D.
>>>
>>> The template syntax is clearer, but I don't know if it is better, 
>>> and/or why. I am a big advocate of clarity in computer languages but 
>>> not if there is any loss of functionality. I do not believe that 
>>> clarity should ever be sacrificed for needed functionality. That is 
>>> one reason I still believe that Java and C# are merely subsets of 
>>> C++, not improvements. Taking away functionality for clarity is never 
>>> the way to go, but adding clarity while keeping, or improving the 
>>> same functionality, as D has done in mostr cases, is admirable. 
>>> However in some areas D seems to have given away functionality for 
>>> clarity, or some holy grail which few understand, as in the const 
>>> debacle.
>>>
>>> I am hoping Walter will address this issue of template functionality 
>>> as opposed to syntax, between C++ and D, extensively sometime in the 
>>> future, hopefully in a technically neutral way ( he tends to be 
>>> biased toward D fro some odd reason <g> ).
>>
>> It would be interesting to see what it takes to reproduce MPL in D.
>>
>> But here's a quickie for you, to implement a "type sequence" like 
>> mentioned here 
>> [http://www.boost.org/doc/libs/1_35_0/libs/mpl/doc/tutorial/representing-dimensions.html] 
>> the code looks basically like this:
>>
>> template boost_mpl_vector(T...) {
>>    alias T boost_mpl_vector;
>> }
>>
>> Then you can do like the first example on that page:
>>
>>     alias boost_mpl_vector!(char, short, int, long) signed_types;
>>
>> The next one, the int_<N> template is probably just this in D:
>>
>> template int_(int N) {
>>    const value = N;
>> }
>>
>> Or maybe it would be
>>
>> struct int_(int N) {
>>    static const value = N;
>> }
>>
>> Then the "vector_c" integral sequence wrapper starts to get 
>> interesting, but is probably something like this:
>>
>> template vector_c(T, Vals...)
>> {
>>    alias ConstList!(int, Vals) vector_c;
>> }
>>
> 
> The ConstList will look something like
> 
>  template ConstList!(T, Vals...) {
>      static if (Vals.length == 0) {
>          alias Tuple!() ConstList;
>      }
>      else {
>          alias Tuple!(int_!(Vals[0]), ConstList!(T, Vals[1..$])) ConstList;
>      }
>  }
> 
> Except it would have to be a little more complicated to make the 
> transition from the type T to int_ or long_ or whetever the actual 
> constant type was.  But that is also pretty easy with static if:
> 
> template ConstType(T) {
>    static if (is(T==int)) { alias int_ ConstType; }
>    static if (is(T==long)) { alias long_ ConstType; }
>    static if (is(T==byte)) { alias byte_ ConstType; }
>   ... etc
> }
> 
> There may be a radically better way to do that whole thing, though, 
> since  Tuple!() can already accept constants in addition to types.  The 
> int_ long_ etc helper types might not be necessary at all.
> 
> 
> You might want to check out std.traits, std.metastrings, std.typecons 
> and std.typetuple from Phobos for some template metacode functions.

I will look into the D libraries more, especially the ones you mention. 
I am just coming to grips with D's templates and have also ordered a 
book on D which had the somewhat misleading title of dealing only with 
Phobos, whereas it seems to deal with the D language itself pretty 
extensively also.

D has some really nice features but it really needs much better language 
documentation to attract programmers, especially C++ programmers. I am 
extremely good in C++ and if I have had many, many struggles trying to 
understand D from the specification. Since I can imagine what a less 
knowledgable programmer must have to go through trying to understand D 
for the first time, I can understand why someone else might give up 
based only on the documentation.

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.

In other words it seems as if D should be able to do some of the Boost 
libraries, which makes C++ so pleasurable to use in general despite the 
difficulty of dealing with C++ templates, fairly easily. At the same 
time most of the Boost libraries are tremendously useful. I will look in 
the Phobos library for their equivalents to see if D has some of the 
same abilities in its library.



More information about the Digitalmars-d mailing list