Fun with templates
TommiT
tommitissari at hotmail.com
Sat Jul 6 05:20:16 PDT 2013
On Saturday, 6 July 2013 at 11:35:28 UTC, Manu wrote:
> 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) {}
That would look more sensible, but I don't think that's
implementable if any template could be used in the place of
'Unqual'. For example:
struct Soo(T, int v)
{}
template Too(T, int x)
{
alias Too = Soo!(T, x * x - 102);
}
void foo(T, n)(Too!(T, n) var)
{}
void main()
{
Soo!(int, 42) var;
foo(var);
}
It would be pretty difficult for the compiler to figure out
during the instantiation of foo that T == int and n == 12. I
don't know, but I suspect it's pretty impossible to implement a
compiler that could figure out those template parameters for
every possible scenario.
More information about the Digitalmars-d
mailing list