top | item 8982882

C++ named operators

113 points| noobermin | 11 years ago |github.com | reply

29 comments

order
[+] yoklov|11 years ago|reply
This is too clever to the point of being painful. (Not to mention, it has a performance penalty for no real benefit.)

I think I'd just give up and go home if I ever saw this in code I had to maintain.

EDIT: Actually, looking more closely, I guess there isn't a performance penalty (I saw back_inserter and assumed it was required for the implementation). That said, I still think it's far too clever.

[+] nmeofthestate|11 years ago|reply
Yes. This is fun, but FAR too clever. I can only speculate at the baffling compile errors... it doesn't bear thinking about. shudder
[+] JoshTriplett|11 years ago|reply
Gah. Nice idea, but the double-operator hack seems quite painful and error prone, especially with operators right in the middle of the precedence hierarchy.

This might be one of the few rare justifications for overloading the comma operator, for syntax similar to that of Haskell's backquote:

    arbitrary+expr ,foo, arbitrary-expr
Then the ,foo, would get processed last, letting the expressions evaluate first, which matches the semantics of Haskell's infixing backquotes (lowest precedence by default).

Even then, though, that would get ugly in a hurry when mixed with function calls, requiring parentheses for sanity.

[+] MaulingMonkey|11 years ago|reply
operator, is one of the operators defining a sequence point (e.g. guaranteeing the left side of the expression executes in whole before the right - others being &&, ||, and ?:.)

Overloading the operator removes this sequence point. Too sketchy for my blood.

Some prior art: http://www.gamedev.net/blog/2/entry-19599-bad-code-here/

[+] Sharlin|11 years ago|reply
* is a better choice if you need something arithmetic-like, for instance

    vector3 a, b;
    float f = a *dot* b;
    vector3 c = a *cross* c;
[+] je42|11 years ago|reply
MMh. You could also this syntax for generating HTML couldn't you ?
[+] EliRivers|11 years ago|reply
The following code is legal C++ and does exactly what you’d expect it to do.

auto result = "Hello" <repeat> 3 <join> ", ";

I had no idea what that might do. No expectations at all. Is this an attempt to write language X in language Y?

[+] epistasis|11 years ago|reply
This is fully explained in the primary link if you read past the tl;dr section.
[+] coldtea|11 years ago|reply
>I had no idea what that might do. No expectations at all.

Really? How is it any different than: "Hello".repeat(3).join(",");

[+] aturek|11 years ago|reply
Does this increase the stack by 2 in every call?
[+] TheCoelacanth|11 years ago|reply
It's a header-only library, so it should be inlined.
[+] wfunction|11 years ago|reply
Terrible idea, terrible implementation, doesn't solve any actual problems...
[+] krotton|11 years ago|reply
Neither does your post. I suppose it was just written for fun or learning experience.