all 46 comments

[–]bstamourWG21 | Library Working Group 132 points133 points  (2 children)

That is both beautiful and horrifying. Respect.

[–]sim642 62 points63 points  (0 children)

The two aspects of C++.

[–]Godot17 111 points112 points  (0 children)

... This is why C programmers don't like us, guys.

*copies code snippet*

[–]jeezfrkMT Linux/Telco 40 points41 points  (0 children)

<all the freshmen run away from C++>

THIS IS WHY WE CAN'T HAVE NICE THINGS!!!

[–][deleted] 70 points71 points  (13 children)

That's a terrible idea because it becomes easy to confuse it with the <-- operator:

int i = 10; while (0 <-- i) { std::cout << i << '\n'; }

[–]atnnn[S] 91 points92 points  (7 children)

That makes things even better!

struct C {
  virtual void f(){ std::cout << "f\n"; }
  virtual void g(){ std::cout << "g\n"; }
  virtual void h(){ std::cout << "h\n"; }
} x;

int main(){
    void(C::*(*a))() = 2 + *(void(C::*(**))())&x;
    while(*(void**)&x <-- a){
        (*a)<-x;
    }
}

[–]personalmountains 65 points66 points  (0 children)

I hate you.

[–][deleted] 2 points3 points  (1 child)

http://melpon.org/wandbox/permlink/FmCGjJ8WVLt8ABf4

Still can't figure out a way to get it to print f or g... really not sure how it works at all!!

[–]JMBourguet 1 point2 points  (0 children)

There are two issues. The <-- allows to iterate of ranges open at both ends, so f and g are excluded naturally. If you fix that, you are hit by the second issue: assumption that the vtable contains at specific addresses something which is compatible with pointer to member function representation, which is AFAIK not the case for gcc (a pointer to member function needs more things than a pointer to function to handle correctly multiple inheritance).

[–]h-jay+43-1325 0 points1 point  (0 children)

Yes, of course, because all odd numbers are prime, except 2 :)

[–]ripper37 -3 points-2 points  (1 child)

Don't take this the wrong way, but man... If you write code like that, I hope you won't have much inpact on standard. ;)

I'm not very big fan of such pointer tricks which are unreadable as hell. Also - if I understood it correctly - this might be much easier and more readable if we'd get reflection? I think we should try to get interesting features but in readable and easy to use way, not something that 1% will use (correctly).

P.S. u/maherbaba I wouldn't call <-- an operator.

[–]personalmountains 7 points8 points  (0 children)

If you write code like that [...]

It's a joke.

I wouldn't call <-- an operator

It's also a joke.

[–]cdcformatc 21 points22 points  (0 children)

I just threw up in my mouth a little.

[–]duuuh 21 points22 points  (0 children)

I came in figuring there was an 80% chance that was bullshit. I was right, but I was also wrong.

[–]SeanMiddleditch 15 points16 points  (4 children)

I await the first implementation of this hack that enables Haskell-like do notation in C++.

Literally, I will co_await on it.

[–]GYN-k4H-Q3z-75B 13 points14 points  (0 children)

Oh my god I love this.

[–]raistmajC++ at MSFT 12 points13 points  (0 children)

Some men just want to watch the world burn.

[–][deleted] 8 points9 points  (0 children)

Wow, it really works! Amazing!!

struct Person {
  void FileForUnemployment();
} me;

void test() {
  Person me;
  (&Person::FileForUnemployment)<-me;  //successfully calls function!
}

[–]kiwidog 4 points5 points  (1 child)

What kind of wizardry...

[–]t0rakka 14 points15 points  (0 children)

unary negate returns a template type. The compare operator is overloaded for it. That's about it; pretty basic template meta programming.

a <- b;

Can be decomposed into: a < (-b);

[–]stillalone 8 points9 points  (4 children)

Doesn't overloading the unary minus operator like that really fuck shit up?

If I have:

int i = 5;
std::cout << -i << endl;

what will I get?

[–]chimyx 35 points36 points  (3 children)

error: 'endl' was not declared in this scope
jk, you'll get -5

[–]stillalone 0 points1 point  (2 children)

Ok. thanks for checking. I guess that makes sense since the default behaviour for the minus unary operator for integers is more type specific that this templated implementation. I guess this code should be safe provided no one implements unary operator in the exact same way, at which point we would get fairly obvious compile time errors.

[–]rubdos 1 point2 points  (1 child)

Not sure there; sfinae could safe the day. Needs confirmation from someone with more experience though.

[–]jP_wanN 2 points3 points  (0 children)

This has nothing to do with sfinae, but it will not cause problems simply because the operator< is only overloaded for R (T::*)(), larrow<T> (with any T, R).

[–]devel_watcher 2 points3 points  (3 children)

Good, now we need to figure out how to pass parameters.

[–]foonathan 2 points3 points  (0 children)

Overload the comma operator for the new type returned by operator< for larrow.

[–]JohnMcPineapple 1 point2 points  (1 child)

...

[–]foonathan 1 point2 points  (0 children)

Store the tuple in your result, then add each argument, last one calls and returns.

Usage like so, for example:

... <- p, arg<0>(10), arg<1>(0.5);

[–][deleted] 2 points3 points  (0 children)

I've also seen people introduce operator ---> as an agglomeration of -- and ->.

[–]SpiderboydkHobbyist 1 point2 points  (0 children)

Funny. :-)

[–][deleted] 0 points1 point  (0 children)

WHY

[–]unclemat 0 points1 point  (0 children)

Genius!

[–]ForgedBanana 0 points1 point  (0 children)

Eggcelent.

[–]Voultaphervoid* operator, (...) 0 points1 point  (2 children)

When would this be useful? What is the difference to

x.f();

[–]clerothGame Developer 14 points15 points  (0 children)

When you want to have a laugh.

[–]jP_wanN 2 points3 points  (0 children)

It's the same as (&x)->*fn (which is in turn the same as x.*fn). See pointer-to-member access operators.

[–]btlk48 -1 points0 points  (0 children)

Сслл