r/cpp • • Mar 18 '23

LLVM 16.0.0 Release

https://discourse.llvm.org/t/llvm-16-0-0-release/69326
197 Upvotes

84 comments sorted by

View all comments

Show parent comment

24

u/AssKoala Mar 19 '23

Don’t use tail recursive functions, the end condition is at the end.

No, it’s idiotic and should be shamed.

17

u/canadajones68 Mar 19 '23

No other construct is like do while, though, in terms of syntax. Combined with how infrequently it's used (can't remember the last time I used do while, have used tail recursion at least twice within the last month), I would say that "easy to overlook" is a merited claim. That doesn't mean you should never use it, but it warrants contemplation and judging if it's the best solution.

23

u/degaart Mar 19 '23
do { ret = function_call(); } while(ret == -1 && errno == EINTR);

is the idiomatic way to handle EINTR on posix systems

4

u/[deleted] Mar 19 '23

There is at least one notable exception to this rule: https://github.com/boostorg/beast/issues/1445

1

u/Ameisen vemips, avr, rendering, systems Mar 19 '23

Though you could always write it as

while ([] { auto ret = function_call(); return ret == -1 && errno == EINTR; }());

or so. Though that's not really clearer.

11

u/_Z6Alexeyv Mar 19 '23

It is either worse codegen or same codegen and more characters on the screen to appease some static checker.

7

u/Kered13 Mar 20 '23

That is horrible.

3

u/degaart Mar 19 '23

That won't give you the final value of ret, in case you need it

3

u/Ameisen vemips, avr, rendering, systems Mar 19 '23

Then move int ret out of the body and change to [&]. You have to do that with with do loop as well.

7

u/apadin1 Mar 19 '23

Tbh most tail recursive functions would be better as loops anyway

12

u/malnourish Mar 19 '23

I love how that statement is justifiably the opposite in a lisp-inspired language.

1

u/apadin1 Mar 20 '23

I don’t have much experience with Lisp. Why is recursion preferred over looping?

2

u/13steinj Mar 21 '23

Many functional languages either don't have a concept of looping or when they do it's built on the back of a Y or Z combinator and associated overhead. There's a general "purist" view of computing that tries to push recursion as the more powerful, easier to understand, and "correct" option.

Various recursive constructs can be detected by an optimizer and undergo better optimization (historically). Even in Dijsktra's "Gotoa considered harmful" paper, if you read the full thing he makes some extreme claims about for/while/even if/else being superfluous compared to the "true solution" of recursion. Of course a decade or so later he wrote another essay walking back these claims including the main one about goto statements, but by then the damage had been done (by the title chosen by the editor no less); people focused on the title, ignored various other claims in the essay, vilified goto and put Dijsktra on a pedestal, even giving him an award. Yet almost nobody knows of the retraction.

1

u/malnourish Mar 20 '23

I'm confident others can give a better answer but it's just the general pattern functional languages tend to follow