Connecting Tech Pros Worldwide Forums | Help | Site Map

Operator overload for generics

teel
Guest
 
Posts: n/a
#1: Sep 26 '07
Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.

Best regards
teel

public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}

public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}

public static ParameterComparisonResult operator
<(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public static ParameterComparisonResult operator
Quote:
>(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}

public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}

private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Sep 26 '07

re: Operator overload for generics


teel,

Since you have no constraints, the only methods/properties that you have
available to you are those that are available to object. Unfortunately,
constraints won't do much for you here, since you can't specify to use
integer OR float.

There isn't really much benefit here to using generics here, since the
constraint system pretty much prevents you from doing what you want.

--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"teel" <tlorek@gmail.comwrote in message
news:1190836986.126483.186310@50g2000hsm.googlegro ups.com...
Quote:
Hi there,
I'm trying to apply "less than" and "more than" operators on the
Generics (class template-like) type objects. Below is the code of my
class representing a parameter that can be any type, but only if it's
float or int the comparison results in true/false-like result, else
its INVALID. I get error:
Operator '<' cannot be applied to operands of type 'T' and 'T' at
line:
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
Could anyone take a look and help me with that? I'd be grateful.
>
Best regards
teel
>
public class Parameter<T>
{
public Parameter(T value, T minValue, T maxValue)
{
m_Value = value;
m_MinValue = minValue;
m_MaxValue = maxValue;
}
>
public T Value
{
get { return m_Value; }
set { m_Value = value < m_MinValue ? m_MinValue : (value >
m_MaxValue ? m_MaxValue : value); }
}
>
public static ParameterComparisonResult operator
<(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs < (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs < (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
>
public static ParameterComparisonResult operator
Quote:
>>(Parameter<Tlhs, Parameter<Trhs)
{
if (lhs is float && rhs is float)
{
if ((float)(object)lhs (float)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else if (lhs is int && rhs is int)
{
if ((int)(object)lhs (int)(object)rhs)
return ParameterComparisonResult.TRUE;
else
return ParameterComparisonResult.FALSE;
}
else
return ParameterComparisonResult.INVALID;
}
>
public enum ParameterComparisonResult
{
INVALID,
TRUE,
FALSE
}
>
private T m_Value;
private T m_MinValue;
private T m_MaxValue;
}
>

Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#3: Sep 26 '07

re: Operator overload for generics



"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.comwrote in
message news:Oa0IUpHAIHA.5980@TK2MSFTNGP04.phx.gbl...
Quote:
teel,
>
Since you have no constraints, the only methods/properties that you
have available to you are those that are available to object.
Unfortunately, constraints won't do much for you here, since you can't
specify to use integer OR float.
>
There isn't really much benefit here to using generics here, since the
constraint system pretty much prevents you from doing what you want.
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>

It's with actual math (+, -, *, /, etc) that generics don't work -- at all!
Quote:
>
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
>
"teel" <tlorek@gmail.comwrote in message
news:1190836986.126483.186310@50g2000hsm.googlegro ups.com...
Quote:
>Hi there,
>I'm trying to apply "less than" and "more than" operators on the
>Generics (class template-like) type objects. Below is the code of my
>class representing a parameter that can be any type, but only if it's
>float or int the comparison results in true/false-like result, else
>its INVALID. I get error:
>Operator '<' cannot be applied to operands of type 'T' and 'T' at
>line:
>set { m_Value = value < m_MinValue ? m_MinValue : (value >
>m_MaxValue ? m_MaxValue : value); }
>Could anyone take a look and help me with that? I'd be grateful.
>>
>Best regards
>teel
>>
> public class Parameter<T>
> {
> public Parameter(T value, T minValue, T maxValue)
> {
> m_Value = value;
> m_MinValue = minValue;
> m_MaxValue = maxValue;
> }
>>
> public T Value
> {
> get { return m_Value; }
> set { m_Value = value < m_MinValue ? m_MinValue : (value >
>m_MaxValue ? m_MaxValue : value); }
> }
>>
> public static ParameterComparisonResult operator
><(Parameter<Tlhs, Parameter<Trhs)
> {
> if (lhs is float && rhs is float)
> {
> if ((float)(object)lhs < (float)(object)rhs)
> return ParameterComparisonResult.TRUE;
> else
> return ParameterComparisonResult.FALSE;
> }
> else if (lhs is int && rhs is int)
> {
> if ((int)(object)lhs < (int)(object)rhs)
> return ParameterComparisonResult.TRUE;
> else
> return ParameterComparisonResult.FALSE;
> }
> else
> return ParameterComparisonResult.INVALID;
> }
>>
> public static ParameterComparisonResult operator
Quote:
>>>(Parameter<Tlhs, Parameter<Trhs)
> {
> if (lhs is float && rhs is float)
> {
> if ((float)(object)lhs (float)(object)rhs)
> return ParameterComparisonResult.TRUE;
> else
> return ParameterComparisonResult.FALSE;
> }
> else if (lhs is int && rhs is int)
> {
> if ((int)(object)lhs (int)(object)rhs)
> return ParameterComparisonResult.TRUE;
> else
> return ParameterComparisonResult.FALSE;
> }
> else
> return ParameterComparisonResult.INVALID;
> }
>>
> public enum ParameterComparisonResult
> {
> INVALID,
> TRUE,
> FALSE
> }
>>
> private T m_Value;
> private T m_MinValue;
> private T m_MaxValue;
> }
>>
>
>

teel
Guest
 
Posts: n/a
#4: Sep 26 '07

re: Operator overload for generics


On 27 Wrz, 00:13, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Quote:
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>
It's with actual math (+, -, *, /, etc) that generics don't work -- at all!
Thanks a lot both of you guys!!
It's working with the IComparable :)

Best regards,
teel

Michael S
Guest
 
Posts: n/a
#5: Sep 27 '07

re: Operator overload for generics


"Ben Voigt [C++ MVP]" <rbv@nospam.nospamwrote in message
news:eGlMSqIAIHA.3716@TK2MSFTNGP03.phx.gbl...
Quote:
>
Actually for this case, generics work very well. Constrain the type T to
implement IComparable, and then use IComparable.CompareTo instead of <,=,>
>
Nice brainwork. I thought the problem was unsolvable.
I had never thought of using IComparable.

- Michael Starberg


Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#6: Sep 28 '07

re: Operator overload for generics



"teel" <tlorek@gmail.comwrote in message
news:1190914793.857817.277950@57g2000hsv.googlegro ups.com...
Quote:
Ok guys, may I ask you another question? My final Parameter class
looks like this:
answered in your new post


Closed Thread