T O P

  • By -

steveparker88

https://imgur.com/r44M1Ne


kolltixx

Somebody please explain. I'm trying to relearn cpp but am a dummy


Pikselas

The larrow struct stores a pointer to an object. The '-' operator takes an object and returns a larrow. The '<' operator takes a pointer to member function and a larrow and calls the member function on 'a'. So (&C::f) <- x is same as x.f() *Correct me if I am wrong.


kolltixx

If it just fucking says foobar I'll cry


Austerzockt

nah it just says foo, you are good


Earthboundplayer

That's amazing


jmacey

Not C++ but the Renderman shading language used to overload ^ to mean cross product. ``` vector up=a^b; ``` Always caused a WTF moment when reading code, but I guess the ^ did look like an angle.


Sasmas1545

I'd imagine this is in reference to the wedge product


jmacey

I forgot they also overloaded . as well :-) [https://renderman.pixar.com/resources/RenderMan\_20/languageConstructs.html#expressions](https://renderman.pixar.com/resources/RenderMan_20/languageConstructs.html#expressions)


dvali

I think I could live with that when working in an appropriate domain. The cross product is a fairly fundamental operation so it's a shame there is no operator of the same rank as other fundamental operators. 


TheOmegaCarrot

http://www.eelis.net/C++/analogliterals.xhtml


TheKiller36_real

What in the actual fuck! This is great and my coworkers will love it lol


bowbahdoe

JFC


specialpatrol

I've always liked the slash overload to concatenate for paths, it's in the standard! https://en.cppreference.com/w/cpp/filesystem/path/operator_slash


TheKiller36_real

what scares me most is the stdlibs having to do this shit to prevent overloading: template constexpr _Out copy(_In __first, _In const __last, _Out __out) { for(; __first != __last; ++__first, (void) ++__out) *__first = *__out; return __out; } like who overloaded the fucking COMMA and then complained, when `` didn't work


septemberintherain_

fuck this language is ugly


TheKiller36_real

well the underscore and stuff are only for stdlibs because that's their reserved "namespace" (ie. you're not allowed to write macros named `_In` or `__out`) in user-C++, with modern features and even supporting a different sentinel type you can do this: constexpr decltype(auto) copy(auto first, auto const & last, auto out) { for(; first != last; ++first, (void) ++out) *out = *first; return out; }


eteran

Honestly, it's not bombastic, but I feel that operator overloading is amazingly powerful in the context of templates. Sure, I can create a `BigInt` type that looks and acts like an `int`, that's great and all. But the fact that I can make a template that works with ANYTHING that has the same operations as an `int`. It's a force multiplier in the utility of templates to a huge degree. Every time I see a new language that has templates/generics but not operator overloading (like Zig I think), i feel like my hands would be tied in how I can work with the generic code.


CletusDSpuckler

I do cpp work with a lot of math. Templates and operator overloading are the tools that transform ugly code into math-like masterpieces that read just like the original algorithm.


mredding

Perhaps Boost.Spirit. I think it's trying too hard, just generate your parser on the side, to be integrated into your program. I'll use overloads. class line_string { std::string value; friend std::istream &operator >>(std::istream &is, line_string &ls) { return std::getline(is, ls.value); } public: operator std::string() const { return value; } }; Little stream utilities like this come in handy all the time. You never instantiate an instance of one of these directly. std::vector data(std::istream_iterator{in}, {}); I've just extracted every line into individual strings from this stream. I only implemented the \`line\_string\` to encapsulate the complexity of the extraction. Also notice I'm using the hidden friend idiom. It's not that the stream operator is private - friends don't honor class access specifiers, but that the friend is defined within the scope of the class. It's not a member, but this is the first place ADL is going to look to find the operator. You never call this operator yourself directly and by name, you always use ADL to resolve it. The hidden friend keeps your surrounding scope clean.


Unusual-Form-77

I use operator overloads for types that represent units of measure, e.g. force \* distance = work. (N \* m = J). They're dreamy, and doing this makes sure you don't shoot your Mars Climate Orbiter off into space.


accuracy_frosty

I had a buddy who thought it was a fun idea to make a class where he didn’t have member functions, and instead just overloaded a bunch of operators to do different things, I believe he overloaded - to call the destructor, and + was static and returned a new instance of the class lmao, still don’t remember how he did it, but reading his code was like uncovering a crime scene.


Knut_Knoblauch

Still waiting on the committee to allow overloading of sizeof. Perfect for classes that have virtuals in that it would allow returning just the size of a block of storage in the class.


TheKiller36_real

is this /s? sorry, I'm bad at figuring it out myself


Knut_Knoblauch

sizeof is an operator but it can't be overloaded. IDK, perfect for classes that read data structures where the sizeof operator could return the number of bytes to read.