Request for a more powerful template specialization feature

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 14 16:04:48 PDT 2017


On Friday, 14 July 2017 at 22:49:18 UTC, data pulverizer wrote:
> On Friday, 14 July 2017 at 22:25:15 UTC, data pulverizer wrote:
>> I am aware that this suggestion touches the language and the 
>> compiler - and may significant implications. I would like to 
>> know whether this could be done without too much effort and 
>> whether it would break anything else?
>>
>> If you are writing lots of overloaded templates, constraints 
>> can have unintended behaviour because you end up telling the 
>> compiler what not to do rather than what to do. The above 
>> Unions are clear and simple, easy to use and should result in 
>> cleaner more robust code.
>
> In addition with template specializations you get constraints 
> for free. If I implement template overloads which are all 
> specializations, the compiler gives me a very informative error 
> if I step outside the pre-defined set of implementations. 
> That's just brilliant - exactly what you want! I immediately 
> know when I see that error what the issue is. Am I being naive? 
> Why are constraints better?

One important characteristic about constraints, is that they are 
not bound to types.
Also they can from conjunctions and disjunctions.
Combined with ctfe they are very flexible and powerful.

I do not know how you would do the same with specializations.
Then again being unfamiliar with template-specializations I might 
be overlooking something.


More information about the Digitalmars-d mailing list