Hacker News new | past | comments | ask | show | jobs | submit login
The long arrow operator in C++ (cukic.co)
177 points by santaclaus on July 13, 2017 | hide | past | favorite | 59 comments



Obligatory "x slides to zero":

  while (x --\
              \
               \
                \
                 > 0)
       printf("%d ", x);


Also, never forget, the C++ Multi-Dimensional Analog Literals:

  assert( ( o-------------o
            |L             \
            | L             \
            |  L             \
            |   o-------------o
            |   !             !
            !   !             !
            o   |             !
             L  |             !
              L |             !
               L|             !
                o-------------o ).volume == ( o-------------o
                                              |             !
                                              !             !
                                              !             !
                                              o-------------o ).area * int(I-------------I) );
http://www.eelis.net/C++/analogliterals.xhtml


This is some serious black magic. I cannot wrap my head around the fact that this is valid C++...


These are "just" some free symbols (o, I, L) used with overloaded operators (!, |, --). The rest is pretty formatting with whitespace which isn't relevant to functionality.


I remember this from a similar example in C:

   #include <stdio.h>
   int main()
   {
       int x = 10;
       while (x --> 0) /* x goes to 0 */
       {
          printf("%d ", x);
       }
   }
(prints 9 8 7 6 5 4 3 2 1 0)

https://stackoverflow.com/questions/1642028/what-is-the-oper...


Yeah, `x --> 0` works. Also `0 <-- x` if you don't want to include zero (and `x --- 0` if you don't have a shift key.)


Works just as well in Java. I'll have to slip this in to my next code review :)


Careful with these jokes -- your coding style nazis will give you a new coding rule in return to prevent this madness in the future!


Or, worse, adopt it as the way to do things.

I blame Haskell, Scala, and Ruby.


> Or, worse, adopt it as the way to do things.

Thompson, Ritchie and Kernighan admit that Unix was a prank

http://www.stokely.com/lighter.side/unix.prank.html


Your description of the result is a valid Lisp expression. . .As long as one has defined a prints function.


You can even approach 0 from left and right simultaneously:

    while (a -->> k --> 0 <-- b <<-- c)
and you can have dashed arrows too:

    k = a <- - - - - b;


In the dashed case, k is going to be zero or one (a<b is true or false). This works, though:

        int a=0, b=4;

        while(a < - - - - --b) {
                printf("b: %d\n", b);
        }


I just spent an hour constructing a class called Morse such that this outputs "HELLO WORLD!":

    Morse m;
    cout << (++++m) << (+m) << (+-++m) << (+-++m) << (---m) << ", "
         << (+--m) << (---m) << (+-+m) << (+-++m) << (-++m) << "!\n";
Damn you, C++! Why won't you let me overload the . operator? ;-)


this is beautiful AND insane bravo


In python there is the term "pythonic" describing if something is in the spirit of the languages founding fathers. As in: "The most pythonic way to write this down is ..." or "Map/filter don't feel very pythonic."

What is the equivalent term or expression for being in the spirit of C++?

The article, while a joke (just like C++ originally, look it up), feels very C++y.


I think the word is "confusing"


I was once taught, "In C, calling a solution 'interesting' is a compliment. In Python, that's an insult. Maybe we should go with "interesting".


"Such-and-such is (not) idiomatic C++"


I don't think there's broadly "idiomatic" C++. There's "idiomatic Google C++" though.


Not in a literal sense of the word, perhaps. But in practice, "idiomatic C++" is taken to mean STL- and Boost-like.


While what's idiomatic is subjective, I think there's plenty that everyone can agree is not idiomatic, so I think GP's quote is still pretty commonly used


I think we can all agree that actively re-purposing the postfix-- operator to perform a dereference on a smart is definitely not idiomatic.

Idiomatic might be fuzzy, but it definitely does not include active attempts obfuscation, malice or jokes. The code in the article could be any of these depending on context.


Yep, no arguments here!


The code from the article isn't merely "not idiomatic C++", but I would go so far as to say it's abused or obfuscated C++.

An example of non-idiomatic C++ might be

    for (int I = 0; I < c.size(); I++) {
        Total += c[I].foo;
    }
...instead of using std::accumulate. The code from the article is (humorously pointing out) something else entirely.


Either you come from a very different community of C++ users than I do, or you are using a different definition of "idiomatic" than I am familiar with, because that loop looks like totally ordinary, everyday C++, and is therefore precisely what I'd call "idiomatic", while I can't recall ever having seen anyone actually use std::accumulate in real life. I suppose this is part of the fun of C++, though, in that it supports a family of distinct but compatible dialects.


"This is not confusing enough to be considered C++"


The article where bjarne describes C++ syntax as a joke and programmer job security is a parody.

http://www.snopes.com/computer/program/stroustrup.asp


Not sure if there is such a thing but if not, maybe it could be "Stroubie".


As in something you could end up in? "Using long arrow operator in production will get you into strouble".


Python has a strong sense of purity. It is a language that really guides you into doing things one way and the community reflect that.

Not so much with C++. There is no one way to do things. Depending on your needs, you will use the language differently. Code focused on performance (ex: game engine) will look very different from server code, or a GUI, or embedded software. And none will be more C++y than the other.


Possibly "Bjarney"


undefined behavior


Python has zen. Zen is the basis of what is considered pythonic. C++ does not have zen; the language was designed by committee.


Not really. C++ was designed by Stroustrup; a committee then modified it somewhat.


Stroustrupian


Cute. Though if I actually saw this in a codebase I might tear my hair out.


Even if it's a Joke, C++ lost me a decade ago...


That's a shame. C++11/14/17 each make the language better and deal with many of the might have pushed someone away in the past.

The typing is getting stronger, the library richer and hard things easier. If you know about or can google RAII and are willing to lookup things on occasion to pick algorithms or just the right member function, code gets very clean. After not too long the looking stops because the std lib is still pretty small compared to other languages.

But if you have already moved to Rust I can't make an objective argument for it.


C++11+ is even more complex than before, so I disagree: C++ is worse now. Almost all the old crap still exists on top of the shiny new stuff.

Designing classes that distinguish & and && properly makes writing new types more difficult. You need even more boilerplate stuff now. Like: I define a '-' operator, but I don't automatically get a '-=' operator, WTF. And now I need to add a && version of the -= and += and = and to invoke move() correctly so the type system is pleased AND it won't crash. This is supposed to be used for application code, so why do we need so much brain capacity to handle it?

And I would really be interested in a survey to find our how many professional C++ programmers know the intricate rules behind overloading with template functions, template classes, inside and outside class scope, with namespaces, with & and &&, with const, with auto and lambda and maybe more etc. This is just too much!

Using classes is a bit better now (if they are written properly), but once you start defining your own types, its no fun anymore.

The nicest I can say about C++ is that it is fun if you want to experiment. But for a production system, it is not a good choice, because it is too complex.


If you aren't writing libraries you rarely need to be concerned with move semantics. At one of my two C++ jobs, literally no class has any && shenanigans and at the other.. we are making libraries, so yeah we care about that, but even then 90% "=default" is all we need.

As for the old and new stuff both being there, yes it us but that doesn't mean you need or should use all of it. The new should replace the old or at least let you clean it up.

It sounds like the code you were forced into was super sloppy. Making move increment and decrement operator is an micro-optimization in the extreme. You can probably safely remove that non-sense and not see any difference if your class implements something like sane value semantics and use RAII. But of course like any language someone can make a big ball of mud when misusing the tools.

I agree there is complexity but I think most of the complexity you have needed to deal with is because of bad code. I could write you some Python or Ruby that is equally impossible to parse and deal with. Watch as I overload methods on Method class and Class class.


I'm not using rust yet, I am mostly writing using higher level languages (python then elixir/erlang, also lua) with a few part in C when I need to do something C is good at.

Also enjoying elm for frontend.


> C++11/14/17 each make the language better and deal

That may be true, but all you are seeing is C++ copying from every other language in an effort to once again become relevant.

IMHO in this modern day and age there are so many easier, better, more expressive, easier to use languages than C++ to choose from.

And that is coming from someone who spent a lot of time coding C++ some 15 years ago.


> an effort to once again become relevant.

I was unaware that being the 2nd and 3rd most popular language made it not relevant when I guess nothing is relevant except Java. (Per the Tiobe index)

If you need high level abstractions and the ability to tweak for performance at the lowest levels the it is either C++ or Rust, and if you need a mature ecosystem that really only leaves C++. Game devs, Google, Facebook, HPC and tons of stuff and now with emscripten and WASM we get to compete with JS devs for in browser games too!


> That may be true, but all you are seeing is C++ copying from every other language in an effort to once again become relevant.

Considering that C++ is perhaps the most popular programming language ever designed (in TIOBE's index it ranks 3rd, right after C and Java), you sound that you nurtured an irrational hatred for a programming language. That says more about you than the language you are trying to criticise.


"all you are seeing is C++ copying from every other language in an effort to once again become relevant." considering just about every other languages interpreter is written in C thats an odd statement. It's like saying the egg copied the Chicken.


> That may be true, but all you are seeing is C++ copying from every other language in an effort to once again become relevant.

To be fair, that's what every other language does too. There are few truly novel ideas in industry programming languages.


and don't forget the equally useful and serious integer unary-double-equal greater-than operator

  8==>


I guess if this was Reddit, we would have had a meme storm by now...


Law of Demeter, much?


Is this an Aprils fools joke posted a few months late?


Why??????????????????????????


"Science isn't about why - it's about why not. Why is so much of our science dangerous? Why not marry safe science if you love it so much?"


It's a joke.


They had me because it was so similar to the - very real - combinations of "conses" in the lisp world: caaaar, cdaddr, cddddr and so on. https://franz.com/support/documentation/6.0/ansicl/dictentr/...


Those who do not fear Poe's Law are doomed to repeat it.


I think you mean "Why??!??!??!??!??!??!??!??!??!" [1]

[1] https://stackoverflow.com/questions/7825055/what-does-the-c-...


Did you overload the "?" operator?


I think the parser will fail anyway. You can't overload syntax.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: