6 ms·
C++ named operators
- je42 12y agoMMh. You could also this syntax for generating HTML couldn't you ?
- cordite 12y agoI don't think it would be as easy as what hamlet [1] looks like, since C++ doesn't have a notion of quasi quoting as far as I am aware, but it might be interesting. [1]: http://www.yesodweb.com/book/shakespearean-templates http://www.yesodweb.com/book/shakespearean-templates
- MaulingMonkey 12y agoI present to you: XSMELL: http://www.gamedev.net/topic/539056-c-bask-in-my-awesomeness/ http://www.gamedev.net/topic/539056-c-bask-in-my-awesomeness...
- je42 12y agoInteresting ! thanks for the pointer !
- MaulingMonkey 12y agoJust promise you won't use it in production on the same continent as me ;)
- je42 12y agoLOL. Don't worry - I don't code in C++ atm.... but when I do have the opportunity to code C++ and need XML i will give it a try.
- frozenport 12y agoImagine if somebody did this for regex!
- MaulingMonkey 12y agoI don't think I'm going to be able to sleep tonight.
- arcatek 12y agoDo you mean Boost Spirit? http://en.wikipedia.org/wiki/Spirit_Parser_Framework http://en.wikipedia.org/wiki/Spirit_Parser_Framework
- JoshTriplett 12y agoGah. 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 12y agooperator, 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/ http://www.gamedev.net/blog/2/entry-19599-bad-code-here/
- Sharlin 12y ago* 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;
- wfunction 12y agoTerrible idea, terrible implementation, doesn't solve any actual problems...
- krotton 12y agoNeither does your post. I suppose it was just written for fun or learning experience.
- EliRivers 12y agoThe 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 12y agoThis is fully explained in the primary link if you read past the tl;dr section.
- MaulingMonkey 12y agoI think the point is that this isn't... necessarily idiomatic C++.
- coldtea 12y ago>I had no idea what that might do. No expectations at all. Really? How is it any different than: "Hello".repeat(3).join(",");
- coldpie 12y agoMostly because the order of operations is unclear. I read it as producing "HelloHelloHello, ". That is, repeat the previous thing three times and then concatenate that with the string ", ". It's surprising to me that "repeat" results in a new object with new semantics.
- ilitirit 12y agoDepends on the language I guess. I read it as "repeat the string Hello thrice and then add a comma and space". It's not really clear that "repeat" returns a list. A C# equivalent would be: String.Join(", ", Enumerable.Repeat("Hello", 3)) Here it's easier to see that Repeat returns an Enumerable object. Using an Extension Method, it's easy to see how someone could mistake the output: https://ideone.com/DuIvZN https://ideone.com/DuIvZN
- borntyping 12y agoI did much the same thing in Python a while ago: https://github.com/borntyping/python-infix https://github.com/borntyping/python-infix
- ericfrederich 12y agoVery cool and elegant; thanks.
- james2vegas 12y agohttps://metacpan.org/release/Sub-Infix https://metacpan.org/release/Sub-Infix
- yoklov 12y agoThis 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 12y agoYes. This is fun, but FAR too clever. I can only speculate at the baffling compile errors... it doesn't bear thinking about. shudder
- aturek 12y agoDoes this increase the stack by 2 in every call?
- TheCoelacanth 12y agoIt's a header-only library, so it should be inlined.