Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As the title says, I find it useful to be able to overload operators. Is it possible to also change the way the operators are parsed by specifying the precedence and associativity of overridden operators?

share|improve this question
5  
Why on earth would you want to do that? – linkerro Aug 16 '12 at 7:52
1  
@linkerro If you're already overriding what the operator does, it makes sense that you might also want to change the order in which they're evaluated. – Bill the Lizard Aug 16 '12 at 13:30
@linkerro as Bill the Lizard says ... – user827992 Aug 16 '12 at 16:05
@MartinBeckett i'm reading, let's see. – user827992 Aug 16 '12 at 16:06
show 1 more comment

3 Answers

up vote 5 down vote accepted

No, you can't do that and that's a good thing. Operator overloading already has enough potential to make code unreadable without being able to change precedence or associativity.

If you even want to do that you are probably abusing operator overloading and should use normal functions instead.

share|improve this answer
1  
-1 Your second paragraph is unnecessarily combative. Ad-hoc operators are immensely valuable for improving the clarity and concision of code through DSLs. The problem with C++ operator overloading is that it’s essentially unrestricted—so your first paragraph is absolutely on point. – Jon Purdy Aug 16 '12 at 22:17

No. While it may at first sight seem to make sense, if you think a little more, it becomes muddy to the point that it's a bad idea.

First note that the language isn't defined by using a precedence grammar, but a more classic BNF. I'm not sure that the behavior of sizeof or ?: is describable simply with a notion of priority and associativity.

But the major issue is that you are changing the priority and associativity of overloaded operators, not defining the priority and associativity of new operators used only for your types. Do you want your changes to be applicable for all uses, or just for your overload? You'll probably agree that the first option is a sure receipt for trouble. The other isn't much better. How to you know it's your overload which should be considered before doing overload resolution, which need parsing and thus associativity and priority to be known.

share|improve this answer

No. Besides making code unreadable, it would make the language more ambiguous and deeply context-dependent because you would not be able to associate parameters with function calls until after you know all the operators available.

Consider the expression

a1 + a2 * a3

where all a* vars have type A and you have overloadings

A operator+(A, A) // low precedence
A operator*(A, A) // high precedence
B operator+(A, A) // high precedence
A operator*(B, A) // low precedence

This could be interpreted two different ways

operator+(a1, operator*(a2, a3))

or

operator*(operator+(a1, a2), a3)

With global precedence and associativity rules, the compiler can commit to the first interpretation during parse, but with overridable associativity/precedence, there's no way to figure out how to decompose tokens into function calls until you know all the available operator signatures.

This doesn't make the language impossible to parse (although there are more programs that have to be rejected as untypable) but it would make it slower to compile, and harder to read.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.