Stroustrup's talk on C++0x

Sean Kelly sean at f4.ca
Sun Aug 26 09:25:33 PDT 2007


Walter Bright wrote:
> Bill Baxter wrote:
>> Walter Bright wrote:
>>> Bill Baxter wrote:
>>
>>>> but some are also available in g++ now, I believe.  And there are 
>>>> some features slated for  C++ 09 that aren't on the roadmap for D at 
>>>> all (like concepts
>>>
>>> Concepts aren't a whole lot more than interface specialization, which 
>>> is already supported in D.
>>
>> I'm not sure what you mean by that, but the feature that I liked most 
>> about it is interface checking.  So
>> A) Being able to document someplace that if you want to use my 
>> KewlContainer you must implement the KewlIteratorConcept which means, 
>> say, you support opPostInc() and opSlice() (for dereferencing as x[]).
>> and then once that is documented
>> B) being able to say that my class implements that concept and have 
>> the compiler check that indeed it does.
>>
>> I suppose there may be some way to do all that in current D,
> 
> Yup:
> 
> interface KewlIteratorConcept
> {
>     T opPostInc();
>     U opSlice();
> }
> 
> class KewlContainer : KewlIteratorConcept
> {
>     T opPostInc() { ... }
>     U opSlice() { ... }
> }
> 
> class WrongContainer
> {
> }
> 
> template Foo(T : KewlIteratorConcept) { ... }

The obvious disadvantage to this approach is that is requires 
implementation of an interface by the creator of the object.  More 
often, I use an additional value parameter to specialize against:

template Foo(T, bool isValid : true = PassesSomeTest!(T)) {}

This also works for non-class types.  I'm not sure I like the syntax 
quite as much as concepts here, but it's good enough that I haven't 
really missed them.


Sean



More information about the Digitalmars-d mailing list