On the richness of C++
Bill Baxter
dnewsgroup at billbaxter.com
Sun Apr 13 21:12:53 PDT 2008
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.
--bb
More information about the Digitalmars-d
mailing list