8 ms·
C++ left arrow operator (2016)
- travisd 3y agoThanks! I hate it!
- rryugciuf 3y agoOh my
- deleted 3y ago[deleted]
- a1o 3y agoLove code made for kicks. Also the other articles on the blog are super different one from the other!
- spacechild1 3y agoThis is pretty evil :-D
- 1letterunixname 3y agoBut is it as evil as? delete this; https://isocpp.org/wiki/faq/freestore-mgmt#delete-this https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
- spacechild1 3y agoIt certainly has its use cases, e.g. classes with intrusive reference count, where the "release" method would destroy the object once the refcount has reached 0.
- amluto 3y agoIs that really a good use case? Suppose you have: struct foo { atomic<int> refcount; // other stuff }; You can pass around a pointer, COM-style, and call ->Release as needed. But you can be a lot less error-prone by passing around a smart pointer that understands the intrusive refcount and handles releasing. At which point you get something like 'delete ptr' in a destructor, not 'delete this' in a Release function.
- spacechild1 3y agoIn practice, people would (hopefully) wrap the whole thing in a smart pointer anyway, instead of manually calling addRef() and release(). But for COM style interfaces you need the release() method because you only want to expose pure virtual interfaces.
- Const-me 3y ago> 'delete ptr' in a destructor, not 'delete this' in a Release function That approach requires client and server to be written in the same language, use the same memory allocator, and same compiler. For COM objects, often all of these are false. For example, C#, PowerShell or VBScript code can call IUnknown.Release() method of C++ implemented COM objects, which will cause C++ code to deallocate the memory. However, these higher level languages can’t directly delete C++ objects: they know nothing about C++ runtime, or C runtime.
- wheybags 3y agoI'm a big fan of: this->~MyClass(); new (this) MyClass(); I have unironically used this
- amluto 3y agoIn a language with affine types (i.e. real move semantics, like Rust but not like C++), destroying this is safe. OTOH deleting, as in freeing memory under the assumption that it was allocated in a particular manner, is only safe if you can somehow enforce that it was allocated that way.
- mgaunard 3y agowhich you can by making the constructor private.
- dzogchen 3y agoOperator overloading is one of those things that needs to be done with good taste.
- TillE 3y agoHonestly I can't think of many notable abuses of operator overloading in C++. The worst is probably the original idea to overload the bit shift operators for stream I/O, something which never really caught on outside the standard library.
- blt 3y agoAgree. Meanwhile there are flagship C++ libraries like Eigen where it's indispensable. C++ has mistakes but operator overloading isn't one of them.
- hawski 3y agoThere could be a couple of examples in Boost like Boost Spirit [0]. Qt had some like putting stuff in containers with bitshift operator. There was a GUI toolkit that used + operator to put widgets on a window. [0] https://www.boost.org/doc/libs/1_78_0/libs/spirit/doc/html/spirit/introduction.html https://www.boost.org/doc/libs/1_78_0/libs/spirit/doc/html/s...
- 1letterunixname 3y agoContinuation passing monads form the basis of a perfectly valid and usable software architecture and programming pattern. In the case of ostream and operator<<, this pattern reduces the number of intermediate objects that would otherwise be constructed. If you object to iostream on religious or stylistic grounds, there's always fmt which is more like Go or Python string interpolation.[0] 0. https://fmt.dev https://fmt.dev
- rwbt 3y ago> If you object to iostream on religious or stylistic grounds, No I object to it on usability grounds. It took more than 30 years for the committee to admit it but C++ now has typesafe std::print/std::format finally, after admonishing programmers for using `printf` in C.
- 1letterunixname 3y agoHaskell, F#, and Scala enter the chat to lend a hand with custom operators.
- Sharlin 3y agoSee also the long right arrow, or "goes to" operator --> (https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c-c https://stackoverflow.com/questions/1642028/what-is-the-oper...)
- alejohausner 3y agoThere’s also the “comes from” operator: while (x < — - 10) { … } In fact, you can make arrows longer: while (x <— — — — - 10) And you can make thicker arrows with plus signs: while (x ++++> 10) (hmmm… the HN text formatter is fighting me. Those are all minus signs)
- wruza 3y agoPrepend two spaces to turn a paragraph into a code block which leaves the text as is. Not code -- Code -- (“ Code --”)
- layer8 3y agoHN doesn't do that. It is your browser or (more likely) the standard text input of your OS.
- WalterBright 3y agoI like the ...___... operator when your code is sinking.
- jeroenhd 3y agoSee also: the tadpole operator (https://devblogs.microsoft.com/oldnewthing/20150525-00/?p=45044 https://devblogs.microsoft.com/oldnewthing/20150525-00/?p=45...) Returns x+1 or x-1 depending on the direction the tadpole swims (-~x to increment x and or ~-x to decrement x). For a short moment I believed this was a real thing, because my programming font has a special ligature for ~- and -~.
- omoikane 3y agoIt's actually useful in a code golf context, because "x * -~y" is two characters shorter than "x * (y + 1)".
- WalterBright 3y agoThe C++ pointer-to-member is a fairly confusing concept. What it actually is is a pair - a pointer to the instance of the struct, and a pointer to a function in that struct. D calls these delegates, and generalizes it to being a pair consisting of a "context pointer" and a "pointer to a function". The neato thing is these do not have to be struct or class member functions. They can be nested functions, where the the "context pointer" is a pointer to the stack frame of the caller. I.e., a pointer to the closure. Hence, these become lambdas. Lamdas and pointers-to-nested functions are completely interchangeable with pointers to members. The caller does not know the difference. In fact, lambdas are far, far more commonly used in D than pointers-to-members.
- boguscoder 3y agoC++ pointers-to-members have no standard structure and are implementation defined. But in no implementations they have pointers to instances inside them. If that was the case Why would you need an instance (again) pointer/reference to use pointer-to-member? In most implementations they just encode an offset
- WalterBright 3y agoThe pointer to instance part is the `this` pointer. > C++ pointers-to-members have no standard structure and are implementation defined. A missed opportunity, though every C++ compiler I've examined did it the same way.
- boguscoder 3y agoBut again, ‘this’ pointer is not in any way stored inside pointer-to-member. Caller has to provide one
- deleted 3y ago[deleted]
- amluto 3y ago> The pointer to instance part is the `this` pointer. What do you mean? The this pointer isn’t computed until you try to call the pointer-to-member-function.
- psyclobe 3y agoC++ is a never ending series of complexities that lead to incredible use cases
- pharrington 3y agoyes, C++, the original comedy programming language.
- classified 3y agoShocking, and very creative. I like it.
- huhtenberg 3y agoThis is neat, clever and revolting in equal measure.
- uwagar 3y agowhen bjarne realised people can swallow this he went on to create the abomination that C++ is today.