"I think that it's extraordinarily important that we in computer science keep fun in computing."
-- Alan J.Perlis
Dedication, SICP

youtube.com/watch?v=7oV7hiAsVT
#cppcon #cpp #SICP

@codewiz are the `auto` for "our types are too long to type" or "we are just lazy" ? πŸ˜‚

@Matthieu Answering your question requires introducing certain... concepts πŸ˜…

@Matthieu Ok, I must do it. It's too elegant. Too beautiful.

That auto in the parameter list is a C++20 shortcut syntax for templates:

template<typename T>
auto fib(T n) {
...
}

He says "C++20 concepts" in the slide because you could also throw in a concept right there:

auto fib(std::integral auto n) {
...
}

And this would _still_ be a template function, just not one that accidentally concatenates strings just because they had an operator+()

@Matthieu Now that I look at it a bit more, fib() being a template is kind of dumb, because immediately afterwards he forces the type to int for the nested lambda fib_iter.

So, even you passed a long long, it would get truncated to an int in the recursive step!

Going generic there would require naming the type, which brings back the full blown template syntax πŸ€¦β€β™‚οΈ

Isn't that elegant?

Follow

@codewiz the hell is he doing creating an std::function there? bonkers! @Matthieu

Β· Β· SubwayTooter Β· 1 Β· 0 Β· 0

@namark @Matthieu He wanted to show that you could do a recursive lambda... It captures itself with [&] πŸ˜–

@codewiz -_- it doen't... you need to declare it separately then assign it to capture it. And it's the worst way to do recursive lambda, the guy needs to learn to scroll in stackoverflow, or some basic functional programming.
@Matthieu

@codewiz I just had to do it:
ix.io/2B03/cpp

It's like exactly the same trick he used with the a and b to optimize fibonacci, except in case of self reference you actually need that trick cause the lambda does not have a type name, while a and b are states and as such belong in the capture list more than the self reference. Sooo much angeeer!

@Matthieu

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.