C++ Weekly - Ep 436 - Transforming Lambda Captures
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.co...
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
► cppunderthesea...
CLion is a cross-platform JetBrains IDE for C and C++ with:
- A smart C and C++ editor to navigate and maintain your code base productively.
- Code analysis with quick-fixes to identify and fix bugs and style inconsistencies.
- An integrated debugger - along with other essential tools from the ecosystem - available
straight out of the box.
- And much more!
jb.gg/clion_ide code: CppWeeklyCLion
Episode details: github.com/lef...
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2...
WANT MORE JASON?
► My Training Classes: emptycrate.com/...
► Follow me on twitter: / lefticus
SUPPORT THE CHANNEL
► Patreon: / lefticus
► Github Sponsors: github.com/spo...
► Paypal Donation: www.paypal.com...
GET INVOLVED
► Video Idea List: github.com/lef...
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cp...
► C++ Best Practices
Amazon Paperback: amzn.to/3wpAU3Z
Leanpub Ebook: leanpub.com/cp...
JASON'S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback: amzn.to/3g6Ervj
Leanpub Ebook: leanpub.com/ob...
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/ob...
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/ob...
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/co...
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/co...
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/co...
► OpCode Puzzlers Book 1
Amazon Paperback: amzn.to/3KCNJg6
Leanpub Ebook: leanpub.com/op...
RECOMMENDED BOOKS
► Bjarne Stroustrup's A Tour of C++ (now with C++20/23!): amzn.to/3X4Wypr
AWESOME PROJECTS
► The C++ Starter Project - Gets you started with Best Practices Quickly - github.com/cpp...
► C++ Best Practices Forkable Coding Standards - github.com/cpp...
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.co...
► Learning C++ Best Practices - www.oreilly.co...
Пікірлер: 27
This is one of those features I love I can use and I hate the confusing way it is written.
had no clue about this
Wow, this is more useful than just the casting part. I find working with the expansion operator very cumbersome; maybe because I haven't studied all the rules. But with this syntax, you're moving the expansion at the top of the lambda, and you're free to use `strlike` as a single object inside the body. Awesome video!
@AtomicAndi
Ай бұрын
Thanks for pointing that out. The video was maybe not so awesome after all, because I missed the point about the expansion being kind of outside
If only I could find more cool variadic templates/fold expressions related stuff like this
@cppweekly
20 күн бұрын
Upcoming talk at CppCon 2024 will dig into some of this stuff.
This is madness!
It's crazy. I have never heard of this particular use of parameter pack expansion. I guess it'll come in handy.
Literally wrote a subsystem for transporting some version information between client and server, which had almost the exact same function and problem as this video. The function in question was just merging single tokens (from constexpr defines, strings at hand in the version constructing class and other string_view like identifier). WIth the lambda capture we can output a total_size from the single tokens (of whatever stringlike type) and create a new string with reserved size onto which to append
Ahh ok, I see now what you were talking about, that is indeed simpler than the way I suggested, cool!
I am lost on all the ellipses unfortunately :( which videos do you cover those on again?
@UsernameUsername0000
Ай бұрын
Look up parameter pack and then fold expressions.
Surely there are reasons why the ... comes before the identifier initializer?
@abhinavk0929
Ай бұрын
...iden is the syntax to inroduce a parameter pack with the name "iden". So 'strlike' is a pack that consists of string_views into the parameter strings.
What about lifetime of string views? In this simple case it's okay. But imagine that you return the lambda (instead of void function).
@cppweekly
20 күн бұрын
Yes, that's a potential problem. kzread.info/dash/bejne/lYmqxtaFhK3LlcY.html
nice!
Does the string_view{...input} make another copy in that lambda function? I can't get it.
@cppweekly
20 күн бұрын
string_view is never a copy, so it's just creating a view (pointer + length) for each string-like thing passed in. This is not 0 cost, necessarily, but it is very low cost. It might incur some strlen calls sometimes, but otherwise should just be copying 2 pointer-sized things per element.
I feel like this is the kind of code you can only read for a about a week after you write it, and then if you read it again at any point after that you get instantly lost in the folding expressions. They are not very intuitive.
Weird example. I'd rather have work_with_stringlike take everything by stringview. How would the code look then?
Wtf is going on in the comments
@majohime
Ай бұрын
At least no bots spouting slurs, still kinda weird
@RomanOrekhov
Ай бұрын
probably the word "captures" triggered the bots
It's this kind of garbage syntax why I've been working on my own language and the issue with strings is why the default string type in my language *is* a string view.
std::size be like "oh yeah why am i needed anyway"
@N....
Ай бұрын
The funny thing about slideware is its selective memory since it's trying to be relevant in other contexts too