Constrained Templates

Don nospam at nospam.com
Mon Jun 14 08:34:58 PDT 2010


Simen kjaeraas wrote:
> Leandro Lucarella <llucax at gmail.com> wrote:
>> it would be nice
>> to have some sort of way to tell the compiler to write the template
>> constraints for us (the obvious ones at least, there might be other
>> template constraints desired besides the ones the ones the compiler can
>> figure out). This way, the errors can be improved without user
>> intervention.
> 
> How's about @optional? Marks an entire function as optional, i.e. will
> not be included if it does not compile.
> 
> struct foo( T ) {
>   @optional
>   void bar( ) { // Will not exist if T cannot be flabbergasted.
>     T tmp;
>     t.flabbergast( );
>   }
> }
> 

This can be trivially shown to be NP-complete.

void bar(T)()
{
    static if ( big_function!T) {
      T t;
      t.flabbergast( );
    }
}

Compiler cannot determine if T needs flabbergast(), unless it evaluates 
big_function. Which can be arbitrarily complicated.

We're taking the approach of making the default error message in such 
cases as helpful as possible.



More information about the Digitalmars-d mailing list