digitalmars.D.learn - Operator overloading
- silverling (6/6) May 10 2007 I have overloaded the multiplication operator like this
- Jarrett Billingsley (14/25) May 10 2007 Use static if and is() inside the function:
- BCS (15/50) May 10 2007 I really wish there was a static switch
- Chris Nicholson-Sauls (3/57) May 10 2007 I'd welcome it, regardless.
I have overloaded the multiplication operator like this opMul(T)(T number){...} opMulAssign(T)(Tn number){...} Now, I want another multiplier to multiply with a specific class (itself) so generally, one would write (after the other overloads) opMult(T:classA)(T multiplier) as it is specified on the language specs http://www.digitalmars.com/d/template.html under the "Specialization" section. However, DMD refuses to compile, saying that "specialization not allowed for deduced parameter T". I'd really hate to have to overload the opMul with real/long/cfloat/classA/etc. How can I fix this?
May 10 2007
"silverling" <este_aqui_ hot_mail.com.remove.underscores> wrote in message news:f1vqkm$11li$1 digitalmars.com...I have overloaded the multiplication operator like this opMul(T)(T number){...} opMulAssign(T)(Tn number){...} Now, I want another multiplier to multiply with a specific class (itself) so generally, one would write (after the other overloads) opMult(T:classA)(T multiplier) as it is specified on the language specs http://www.digitalmars.com/d/template.html under the "Specialization" section. However, DMD refuses to compile, saying that "specialization not allowed for deduced parameter T". I'd really hate to have to overload the opMul with real/long/cfloat/classA/etc. How can I fix this?Use static if and is() inside the function: blah opMul(T)(T number) { static if(is(T : classA)) { // code for classA version } else { // for other types } }
May 10 2007
Jarrett Billingsley wrote:"silverling" <este_aqui_ hot_mail.com.remove.underscores> wrote in message news:f1vqkm$11li$1 digitalmars.com...I really wish there was a static switch static switch(T) { case classA: { } case int: { } default: { } } fall thought wouldn't work for it though so it would be kind of odd.I have overloaded the multiplication operator like this opMul(T)(T number){...} opMulAssign(T)(Tn number){...} Now, I want another multiplier to multiply with a specific class (itself) so generally, one would write (after the other overloads) opMult(T:classA)(T multiplier) as it is specified on the language specs http://www.digitalmars.com/d/template.html under the "Specialization" section. However, DMD refuses to compile, saying that "specialization not allowed for deduced parameter T". I'd really hate to have to overload the opMul with real/long/cfloat/classA/etc. How can I fix this?Use static if and is() inside the function: blah opMul(T)(T number) { static if(is(T : classA)) { // code for classA version } else { // for other types } }
May 10 2007
BCS wrote:Jarrett Billingsley wrote:I'd welcome it, regardless. -- Chris Nicholson-Sauls"silverling" <este_aqui_ hot_mail.com.remove.underscores> wrote in message news:f1vqkm$11li$1 digitalmars.com...I really wish there was a static switch static switch(T) { case classA: { } case int: { } default: { } } fall thought wouldn't work for it though so it would be kind of odd.I have overloaded the multiplication operator like this opMul(T)(T number){...} opMulAssign(T)(Tn number){...} Now, I want another multiplier to multiply with a specific class (itself) so generally, one would write (after the other overloads) opMult(T:classA)(T multiplier) as it is specified on the language specs http://www.digitalmars.com/d/template.html under the "Specialization" section. However, DMD refuses to compile, saying that "specialization not allowed for deduced parameter T". I'd really hate to have to overload the opMul with real/long/cfloat/classA/etc. How can I fix this?Use static if and is() inside the function: blah opMul(T)(T number) { static if(is(T : classA)) { // code for classA version } else { // for other types } }
May 10 2007