variadic templates and out/inout

Tyro ridimz at yahoo.com
Tue Dec 19 01:53:28 PST 2006


Kirk McDonald wrote:
> Tyro wrote:
>> The following compiles without any errors or warning. However it
>> fails if line 12 is uncommented (error messages provided below). Is
>> there a compelling reason why this should not work? If not, what
>> are the possibilities of getting this implemented?
>>
>> void get(A...)(inout A a)
>> {
>>   foreach(inout t; a)
>>   {
>>     if(typeid(typeof(t)) is typeid(double))
>>       t = 0.0;
>>   }
>> }
>>
>> void main()
>> {
>>   double d;
>>   //get(d); // fails
>> }
>>
>>
>> /+
>> Error messages:
>>
>> io.d(3): no storage class for t
>> io.d(12): template instance io.get!(double) error instantiating
>> +/
>>
>> ------------
>> Andrew Edwards
> 
> There is no particular reason why this shouldn't work. However, there is 
> a very simple workaround. Re-write 'get' like this:
> 
> void get(A...)(inout A a)
> {
>   foreach(i, t; a)
>   {
>     if(typeid(typeof(t)) is typeid(double))
>       a[i] = 0.0;
>   }
> }
> 

Further evaluation of the above has revealed that the assignment a[i] = 
0.0 is attempted prior to if(...) being evaluated. This causes the 
function to fail if the type of the argument passed in does not match 
that of the value being assigned. Is there any way around this?

-- 
Andrew C. Edwards
-----------------------------------------------------
The truth we call D, has passed through three stages:
   First, it was ridiculed;
   Then, it was violently opposed; and
   And now, it is being accepted as self-evident.
Consequently:
   C/C++ is rapidly approaching Stage 5 (being forgotten)!



More information about the Digitalmars-d mailing list