Problem about Tuple.opEquals, const qualifier

Simen Kjærås simen.kjaras at gmail.com
Sun Mar 18 04:13:50 PDT 2012


On Sun, 18 Mar 2012 08:34:19 +0100, Tongzhou Li <zhangsongcui at hotmail.com>  
wrote:

> On Sunday, 18 March 2012 at 06:15:16 UTC, Ali Çehreli wrote:
>> Every template instantiation of a set of template parameters becomes a  
>> distinct type than any other set of template parameters.
>>
>> In other words, Tuple!(double, char) and Tuple!(int, char) are distinct  
>> types. For all the compiler knows, Tuple is a user type and only the  
>> user should define whether they are compatible. Tuple does not define  
>> opCast() to covert from Tuple!(int,char) to Tuple!(double,char). I  
>> guess it could have been defined. (?)
>>
>> A solution is to explicitly perform the conversion:
>>
>> import std.conv;
>> // ...
>>     stack ~= to!(Tuple!(double, char))(tuple(10, '+'));
>>
>> (An alias would make that easier to read. :/).
> It seems long and ugly...
> I also write:
>      Tuple!(uint, double delegate(double, double))[char] Operators;
>      Operators['+'] = tuple(1u, (x, y) => x + y);
> It doesn't work. I have to write:
>      Operators['+'] = tuple(1u, cast(double delegate(double, double))(x,  
> y) => x + y);
> Much longer :/
>
> Someone tell me that this is called SFINAE problem in C++
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html
> I tried this C++ code:
>      stack<pair<double, char> > s;
>      s.push(make_pair(10, '+'));
> It compiles fine with g++.
> Any chance to see this being solved in D?
> ;)

Try this one for size:


import std.traits : isImplicitlyConvertible, isSomeChar, isAssignable;
import std.conv : to;

void push( T, U )( ref T[] arr, U element ) if  
(isImplicitlyConvertible!(U, T) ||
             (isSomeChar!T && isSomeChar!U) || isAssignable!(T, U) ) {
     arr ~= to!T( element );
}


Works for me with this code:

Tuple!(double, char)[] arr;
arr.push(tuple(10, 'a'));


More information about the Digitalmars-d-learn mailing list