Article on Tuples

Sean Kelly sean at f4.ca
Fri Nov 17 07:56:49 PST 2006


Bill Baxter wrote:
> Oskar Linde wrote:
>> Sean Kelly wrote:
>>
>>> Walter Bright wrote:
>>>
>>>> Sean Kelly wrote:
>>>>
>>>>> Ack! Related question :-)  I assume this will work at some point?:
>>>>>
>>>>>   T fn( T, U )( U val )
>>>>>   {
>>>>>       return T.init;
>>>>>   }
>>>>>
>>>>>   void main()
>>>>>   {
>>>>>       int i = fn!(int)( 1.0 );
>>>>>   }
>>>>>
>>>>> Currently, I get:
>>>>>
>>>>>   test.d(9): template instance fn!(int) does not match any template 
>>>>> declaration
>>>>>   test.d(9): Error: template instance 'fn!(int)' is not a variable
>>>>>   test.d(9): Error: function expected before (), not fn!(int) of 
>>>>> type int
>>>>
>>>>
>>>> That's a hard one to get to work, as it has chicken-and-egg problems.
>>>
>>>
>>> Hrm... I think that one may eventually turn out to be fairly 
>>> important.  It's quite common to specify only the return type for 
>>> template functions in C++.  The most obvious example being the cast 
>>> functions.
>>
>>
>> You can use nested templates as a workaround:
>>
>> template myCast(T) {
>>   T myCast(U)(U val) {
>>     return cast(T) val;
>>   }
>> }
>>
>> void main() {
>>   int i = myCast!(int)(1.0);
>> }
> 
> 
> Bummer, that doesn't seem to work inside a class member template:
> 
> class Foo {
>     template myCast(T) {
>         T myCast(U)(U val) {
>             return cast(T) val;
>         }
>     }
> }
> 
> void main() {
>   Foo foo = new Foo;
>   int i = foo.myCast!(int)(1.0);
> }
> 
> -->
> Error: function expected before (), not 'foo dotexp template myCast(U)'

Hopefully, this is just a bug.  If it can be made to work I don't see a 
pressing need for partial template argument evaluation.


Sean



More information about the Digitalmars-d-announce mailing list