133
u/bstamour WG21 | Library Working Group Jul 29 '16
That is both beautiful and horrifying. Respect.
62
111
39
u/jeezfrk MT Linux/Telco Jul 29 '16
<all the freshmen run away from C++>
THIS IS WHY WE CAN'T HAVE NICE THINGS!!!
21
21
u/duuuh Jul 30 '16
I came in figuring there was an 80% chance that was bullshit. I was right, but I was also wrong.
16
u/SeanMiddleditch Jul 29 '16
I await the first implementation of this hack that enables Haskell-like do notation in C++.
Literally, I will co_await on it.
0
Aug 03 '16
[deleted]
3
u/SeanMiddleditch Aug 03 '16
... that joke went right over someone's head. :p
Also, while coroutines (P0053) are not going into C++17, they're on track for C++2x (with a pitstop in a TS, most likely) with strong committee interest (likely incorporating P0073 after the review in Oulu), so I'll just "await" on the TS landing. ;)
0
Aug 04 '16
[deleted]
2
u/SeanMiddleditch Aug 04 '16
While I wasn't at the Oulu meeting, the meeting notes tell quite a different story. :)
The syntax is not under debate. It's very useful and handy for some domains and use cases. The underlying mechanism for how it works and whether it's layered over this library abstraction or that library abstraction is up for debate.
If nothing else,
awaitjust becomes a library function.awaitis a feature that any language with any complete form of coroutines (or even just generators) can trivially supply.
15
13
9
Jul 30 '16
Wow, it really works! Amazing!!
struct Person {
void FileForUnemployment();
} me;
void test() {
Person me;
(&Person::FileForUnemployment)<-me; //successfully calls function!
}5
u/kiwidog Jul 29 '16
What kind of wizardry...
15
u/t0rakka Jul 29 '16
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);
7
u/stillalone Jul 29 '16
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?
35
u/chimyx Jul 29 '16
error: 'endl' was not declared in this scope
jk, you'll get -51
u/stillalone Jul 29 '16
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.
2
u/rubdos Jul 30 '16
Not sure there; sfinae could safe the day. Needs confirmation from someone with more experience though.
3
u/jP_wanN Jul 30 '16
This has nothing to do with sfinae, but it will not cause problems simply because the
operator<is only overloaded forR (T::*)(), larrow<T>(with any T, R).
3
u/devel_watcher Jul 30 '16
Good, now we need to figure out how to pass parameters.
3
u/foonathan Jul 30 '16
Overload the comma operator for the new type returned by operator< for larrow.
2
u/JohnMcPineapple Jul 30 '16 edited Oct 08 '24
...
2
u/foonathan Jul 31 '16
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);
3
2
1
1
1
1
u/Voultapher void* operator, (...) Jul 30 '16
When would this be useful? What is the difference to
x.f();14
3
u/jP_wanN Jul 30 '16
It's the same as
(&x)->*fn(which is in turn the same asx.*fn). See pointer-to-member access operators.
0
71
u/[deleted] Jul 29 '16
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'; }