Fun with templates

Manu turkeyman at gmail.com
Sat Jul 6 04:35:14 PDT 2013


On 6 July 2013 21:23, TommiT <tommitissari at hotmail.com> wrote:

> On Saturday, 6 July 2013 at 10:34:04 UTC, Manu wrote:
>
>> I'm getting the distinct feeling that what I want is not actually
>> possible.
>> Which I have to say, is rather surprising.
>>
>
> This seems like a defect of the language. Perhaps a new language feature
> is needed. Something like this:
>
> void foo(inout T)(T t)
> {
>     t.mutate();
> }
>
> Pass a variable of type S, const(S), or mutable(S), and the function
> signature becomes:
>
> void foo(S);
>
> And then fails if your argument cannot be converted to mutable S.
>

The way that makes the most sense to me is:

void f(T)(Unqual!T t) {}

Given an immutable(T) for instance that I want to call with, it would
instantiate the template with the signature void f(T), and then attempt to
call it with the immutable(T). If immutable(T) is not convertible to the
argument T, then it would produce a compile error as if attempting to call
any such function that just receives T.

The point here is that I want more control over the signature of the
instantiated template (reduce the number of permutations generated for
various calls). Template blow-out is perhaps the biggest and most well
known day-to-day problem in C++, and tools like this may be very valuable
to mitigate the disaster.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130706/8e8be9d7/attachment.html>


More information about the Digitalmars-d mailing list