void myFunc(T: !=int)(T value)

Sean Kelly sean at invisibleduck.org
Fri Aug 22 12:53:35 PDT 2008


tsalm wrote:
> Le Fri, 22 Aug 2008 15:23:11 +0200, Steven Schveighoffer 
> <schveiguy at yahoo.com> a écrit:
> 
>> "tsalm" wrote
>>> Le Fri, 22 Aug 2008 14:33:23 +0200, Jarrett Billingsley
>>> <kb3ctd2 at yahoo.com> a écrit:
>>>
>>>> -------------------------------------------
>>>> "tsalm" <tsalm at free.fr> wrote in message
>>>> news:op.ugacfwj7010shu at papillon.lan...
>>>> Hello,
>>>>
>>>> I want to do something like this :
>>>>
>>>> class MyClass
>>>> {
>>>>    void myFunc(int intValue)
>>>>    {
>>>>      /* Do something with this int */
>>>>    }
>>>>
>>>>    void myFunc(T: !=int)(T valueNotInt)
>>>>    {
>>>>      /* valueNotInt can not be an int */
>>>>    }
>>>> }
>>>>
>>>> Thanks in advance,
>>>> TSalm
>>>> -------------------------------------------
>>>>
>>>> void myFunc(T: int)(int intValue) {}
>>>> void myFunc(T)(T notIntValue) {}
>>>>
>>>> :)
>>>>
>>>
>>> In fact, the exact operator is :
>>>   int opCmp(MyStruct m)
>>>
>>> It must be use, when this struct is in an array, to sort this array
>>> And strangly, the method
>>>    int opCmp(T:MyStruct)(MyStruct m)
>>> is not take by ".sort"
>>> :(
>>
>> Ah, your problem is that template methods cannot be virtual.  .sort is
>> trying to use the virtual method int opCmp(Object) in Object.
>>
>> Overload with Object as the argument for the non-int function:
>>
>> int opCmp(Object o); // this will be used in .sort
>> int opCmp(int i);
>>
>> -Steve
>>
>>
> 
> I don't explain myself clearly ...
> 
> This problem concern a "bug" on tango :
> http://www.dsource.org/projects/tango/ticket/1259#preview
> 
> Here's a better example :
> 
> /***************************************/
> 
> struct MyStruct
> {
>  int a;
> 
>  // Must be use to sort this struct on an array
>  // but the compilation send a conflict with "opCmp(T)(T val)"

You aren't currently allowed to overload template and non-template 
functions.  It's an annoying limitation that is either already fixed or 
will be fixed in D 2.0.


Sean


More information about the Digitalmars-d-learn mailing list