C++ Weekly - Ep 344 - decltype(auto): An Overview of How, Why and Where

  Рет қаралды 14,268

C++ Weekly With Jason Turner

C++ Weekly With Jason Turner

Жыл бұрын

☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.com/workshops/c-b...
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024
► cppunderthesea.nl/workshops/
► 25% of your first year of UltraEdit: www.ultraedit.com/catalog/#?d...
► For show notes: github.com/lefticus/cpp_weekl...
Ep 287 - Understanding `auto` - • C++ Weekly - Ep 287 - ...
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-sprin...
WANT MORE JASON?
► My Training Classes: emptycrate.com/training.html
► Follow me on twitter: / lefticus
SUPPORT THE CHANNEL
► Patreon: / lefticus
► Github Sponsors: github.com/sponsors/lefticus
► Paypal Donation: www.paypal.com/donate/?hosted...
GET INVOLVED
► Video Idea List: github.com/lefticus/cpp_weekl...
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cpp23_best_practi...
► C++ Best Practices
Amazon Paperback: amzn.to/3wpAU3Z
Leanpub Ebook: leanpub.com/cppbestpractices
JASON'S PUZZLE BOOKS
► Object Lifetime Puzzlers Book 1
Amazon Paperback: amzn.to/3g6Ervj
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/objectlifetimepuz...
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/copyandreferencep...
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/copyandreferencep...
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/copyandreferencep...
► OpCode Puzzlers Book 1
Amazon Paperback: amzn.to/3KCNJg6
Leanpub Ebook: leanpub.com/opcodepuzzlers_book1
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-best-practices...
► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices...
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/...
► Learning C++ Best Practices - www.oreilly.com/library/view/...

Пікірлер: 57
@ohwow2074
@ohwow2074 Жыл бұрын
Wow C++ is so complicated these days. Great content by the way.
@vladigr1
@vladigr1 Жыл бұрын
none forces you to used the complicated stuff.
@myusernameislongerth
@myusernameislongerth Жыл бұрын
@@vladigr1 That's an option if you're the only one writing the code. Or when the entire team chooses to not use it. Otherwise you at least need to be able to read and understand the complicated stuff.
@Minty_Meeo
@Minty_Meeo 8 ай бұрын
C++'s accommodations for generic code are wild.
@zamf
@zamf Жыл бұрын
I've got one question: why the hell does "decltype((variable))" get deduced as a reference? Edit: Once I saw some of the other comments I think `(variable)` deserves its own episode on this channel.
@BenjaminBuch
@BenjaminBuch Жыл бұрын
The answer is at 6:55. "variable" is a simple lvalue, while "(variable)" is an expression which refers to variable.
@zamf
@zamf Жыл бұрын
@@BenjaminBuch That's what I thought but xvalues decay to an Rvalue refrerences while decltype((variable)) deduces an Lvalue reference. That's what confused me.
@BenjaminBuch
@BenjaminBuch Жыл бұрын
@@zamf I'm not completely sure, but I believe "(variable)" is a glvalue, not a xvalue. C++ value categories are tricky ^^`
@LEpigeon888
@LEpigeon888 Жыл бұрын
Both "var" and "(var)" are lvalues, it's just that decltype has a special rule for id-expressions. An id-expression is an expression that only contains a name. I can't post links but everything is explained in the page for decltype in cppreference.
@BenjaminBuch
@BenjaminBuch Жыл бұрын
@@LEpigeon888 Thanks for explanation and link!
@ohikesi
@ohikesi Жыл бұрын
Thank you so much this helped a lot!!!! You saved my life
@treyquattro
@treyquattro Жыл бұрын
thanks for this. decltype(auto) still confuses me. Well, until this video it did!
@friedkeenan
@friedkeenan Жыл бұрын
Might've been good to show what people were using before decltype(auto), i.e. having decltype(appropriate expression here) frequently.
@X_Baron
@X_Baron Жыл бұрын
Yeah, kind of looks like creative abuse of reserved keywords until you stop to think where it's derived from. 🙂
@tamaldas7884
@tamaldas7884 Жыл бұрын
Works well!! DANKEEE
@user-tv5bx5ov5q
@user-tv5bx5ov5q Жыл бұрын
thank you bro thank you
@ultimatesoup
@ultimatesoup 9 ай бұрын
I would like to see an episode on the difference between decltype and declvar
@cppweekly
@cppweekly 9 ай бұрын
I keep track of all the topic requests here: github.com/lefticus/cpp_weekly/issues/ Please add yours and vote on the others!
@dpr8181
@dpr8181 Жыл бұрын
it work on my pc thx bro vеry much
@tux1968
@tux1968 Жыл бұрын
What is the best tool to lint C++ code, and catch things like parenthesis after a return statement, and enforce other best practices?
@PaulMetalhero
@PaulMetalhero Жыл бұрын
clangd?
@tux1968
@tux1968 Жыл бұрын
@@PaulMetalhero Honest question, does it have a best-practices linting mode?
@PaulMetalhero
@PaulMetalhero Жыл бұрын
Also check the FAQ page
@slipcurve1410
@slipcurve1410 Жыл бұрын
You need to configure your clang-tidy and tell it what to lint. If you have lsp supporting editor, like neovim or vscode, clangd is the language server you use. Clang-format is about intendation and stuff like that.
@dadisuperman3472
@dadisuperman3472 Жыл бұрын
I want to talk about casting a lambda into a function pointer, and it's conditions But i think you're better presenter than me. Can you do it?
@cppweekly
@cppweekly Жыл бұрын
I an episode tracker to help me keep track of viewers' requests. Please add yours here: github.com/lefticus/cpp_weekly/issues/
@leonmunster8972
@leonmunster8972 Жыл бұрын
I don't really understand why i != (i). Why are the types different??
@von_nobody
@von_nobody Жыл бұрын
this is `decltype` need support checking of type of expressions like `decltype(1 + 2)` and as C++ support code like `(i) = 42;` in this case `i` need be reference to allow assignment. To sum up, `decltype(i)` is "check what type given name have" and `decltype((i))` is "check what type of given expression is"
@zamf
@zamf Жыл бұрын
@@von_nobody Why does C++ support `(i) = 42;` ? Is it because of C backwards compatibility?
@ABaumstumpf
@ABaumstumpf Жыл бұрын
@@zamf "Is it because of C backwards compatibility?" Na, it is cause there is nothing that says it should not be supported. " `(i) = 42;`" is just a simple case of a more general usecase. This tiny snipped might seem pointless, but that is often the case with simple examples.
@zamf
@zamf Жыл бұрын
@@ABaumstumpf I've heard that allowing this kind of syntax leads to some very subtle bugs. For example: std::mutex m; void f() { std::scoped_lock (m); //you think you've locked the mutex but in fact you've created a temporary scoped_lock with the same name as the mutex and it does nothing } I don't see why anyone would use `(variable)` syntax either in declaration or in usage.
@Dziaji
@Dziaji Жыл бұрын
@@zamf technically, both i and (i) are references. They are only different when deducing return type. I believe the main reason they are different is to allow you to chose whether you want to include the "ref" or not. For example, if you have code like this (obviously you wouldn't, but for more generic code, you might): struct myClass { int a; decltype(auto) getA() { return a; }; }; You are now returning the value of "a", but if you want to return a ref to "a", you do "return (a);" If the code is very generic, you might not know whether "a" is a value or a reference, and treating "a" different than "(a)" gives you the flexibility to force the return type to be a reference. In a somewhat similar but opposite way, you can use "auto(a)" when passing to a const ref parameter to force a copy of "a" rather than using a const ref to "a" itself, if "a" might change during the executing of the function. So I believe this is just a feature of decltype to allow for fine tuning and flexibility rather than an effect of the language itself prior to decltype. I could be wrong about this though.
@teenspirit1
@teenspirit1 Жыл бұрын
Sorry to sound negative, and I have longed for all the compile time richness that C++20 has for the past 20 years. But I hope decltype never sees widespread use.
@treyquattro
@treyquattro Жыл бұрын
decltype has been around since C++11 so it already has seen widespread use
@Dziaji
@Dziaji Жыл бұрын
As explained in this video, it is necessary because plain "auto" discards cv & ref qualifiers, so you need decltype to retain them.
@ABaumstumpf
@ABaumstumpf Жыл бұрын
@@treyquattro decltype(auto) is C++14 - and more and more edgecases and strange behaviours got added later on as well.
@mocloun3709
@mocloun3709 Жыл бұрын
@@Dziaji Then don't use auto!
@Lucifer-yw8yp
@Lucifer-yw8yp Жыл бұрын
your ti to explain sotNice tutorialng
@anon_y_mousse
@anon_y_mousse Жыл бұрын
I haven't used UltraEdit in years. Didn't even know it was still being updated either. I think the last time I ever used it when I was still using Win98. It's not a bad editor, but at the time Crimson Editor was better. Looks like they added a bunch of features to UE and now Crimson is dead.
@LOL-fk4pt
@LOL-fk4pt Жыл бұрын
god
@qualityprice353
@qualityprice353 Жыл бұрын
I dоwnloaded everything is okay
@alexkfridges
@alexkfridges Жыл бұрын
C++ is outrageous lmao.
@mocloun3709
@mocloun3709 Жыл бұрын
Just don't use auto as the return type (write the real return type) and you don't need decltype(auto)!
@animeverse6000
@animeverse6000 Жыл бұрын
kzbin.info/www/bejne/lamVaIawYraekK8
@cppweekly
@cppweekly Жыл бұрын
As stated in the video and in other replies - this is a tool for generic programming. When in generic programming you don't know the return type.
@pw1169
@pw1169 Жыл бұрын
9 minutes of why C++ is such a horrible language! Great vid as usual :)
@animeverse6000
@animeverse6000 Жыл бұрын
kzbin.info/www/bejne/lamVaIawYraekK8
@jplflyer
@jplflyer Жыл бұрын
Okay this drive for always use auto just seems ridiculous. "Hey, we're a strongly typed language, but we want to give up the whole reason we're strongly typed -- so programmers know what the heck they're working with. But because we just obfuscated our code, let's add some more obfuscation to un-obfuscate the situation."
@cppweekly
@cppweekly Жыл бұрын
in the cases where you need decltype(auto) (in generic code, as strongly stated in the video) there is no viable alternative. It's a tool that exists for generic programming.
@jplflyer
@jplflyer Жыл бұрын
@@cppweekly One of the things I love about programming is that we have so many different tools and so many different ways to do things. And we can each find the tools that most fit our mindset. So it's okay when people disagree about the tools to use. Always Use Auto is an example of things we can disagree about, and that's okay. So maybe I really missed something, but I got the impression that we need decltype to make up for using auto. If it weren't for auto, we wouldn't need decltype. But I could just have entirely missed the entire point.
@Voy2378
@Voy2378 Жыл бұрын
This syntax is like a bad joke. It is laughable that they thought adding garbage like this was acceptable in 2014. So happy Rust and Carbon happened.
@stefanalecu9532
@stefanalecu9532 25 күн бұрын
Rust is a different flavor of bad and bloated, but you're not willing to admit it :)
@ervinclarkrendon612
@ervinclarkrendon612 Жыл бұрын
thank you bro thank you
@UltraTV95
@UltraTV95 Жыл бұрын
it work on my pc thx bro vеry much
C++ Weekly - Ep 322 - Top 4 Places To Never Use `const`
18:53
C++ Weekly With Jason Turner
Рет қаралды 35 М.
C++ Weekly - Ep 313 - The `constexpr` Problem That Took Me 5 Years To Fix!
26:19
C++ Weekly With Jason Turner
Рет қаралды 23 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 10 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 13 МЛН
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 19 МЛН
C++ Weekly - Ep 348 - A Modern C++ Quick Start Tutorial - 90 Topics in 20 Minutes
20:47
C++ Weekly With Jason Turner
Рет қаралды 34 М.
C++ Weekly - Ep 425 - Using string_view, span, and Pointers Safely!
8:36
C++ Weekly With Jason Turner
Рет қаралды 12 М.
C++ Weekly - Ep 288 - Quick Perf Tip: Prefer `auto`
10:18
C++ Weekly With Jason Turner
Рет қаралды 15 М.
C++ Insights - Episode 51: The magic of C++20s std::source_location
5:07
C++ Weekly - Ep 343 - Digging Into Type Erasure
15:16
C++ Weekly With Jason Turner
Рет қаралды 18 М.
C++ Weekly - Ep 434 - GCC's Amazing NEW (2024) -Wnrvo
10:19
C++ Weekly With Jason Turner
Рет қаралды 15 М.
C++ Weekly - Ep 373 - Design Patterns in "Modern" C++ (2023)
16:49
C++ Weekly With Jason Turner
Рет қаралды 31 М.
The "auto" keyword in C++
17:04
The Cherno
Рет қаралды 201 М.
C++ Weekly - Ep 334 - How to Put a Lambda in a Container
11:51
C++ Weekly With Jason Turner
Рет қаралды 15 М.
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,6 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 64 МЛН
My iPhone 15 pro max 😱🫣😂
0:21
Nadir Show
Рет қаралды 2 МЛН