Тёмный

Back to Basics: Initialization in C++ - Ben Saks - CppCon 2023 

Подписаться
Просмотров 15 тыс.
% 362

cppcon.org/
---
Back to Basics: Initialization in C++ - Ben Saks - CppCon 2023
github.com/CppCon/CppCon2023
C++ has many ways to initialize objects, and even experienced C++ programmers often have difficulty remembering exactly what each one means. For example, which constructor of T does each of the following statements invoke?
T t1(1, 2, 3);
T t2{4, 5, 6};
T t3 = t2;
Moreover, the context of the initialization can affect how the compiler interprets certain constructs. As such, we often have difficulty deciding which form of initialization to use. Choosing a form of initialization is especially difficult when we don’t know the exact type of the object that we’re initializing (i.e., when the type of the object is a template type parameter).
In this session, we’ll explore the similarities and differences among each form of C++ initialization and how the initialization rules have changed over time. Focusing on the common elements, we’ll see how C++’s initialization rules are (while not simple) not quite as complex as they might first appear. We’ll see how the Standard Library chooses which form of initialization to use and how that affects similar code that you might write yourself. We’ll also discuss how you can design your classes to make them easy to use in light of the initialization rules.
You’ll leave this session with a clearer understanding of exactly what each form of initialization means. With this knowledge, you’ll be better able to decide when each form of initialization suits your needs, which will help you write code that’s more expressive, robust, and maintainable.
---
Ben Saks
Ben Saks is the chief engineer of Saks & Associates, which offers training and consulting in C and C++ and their use in developing embedded systems. Ben has represented Saks & Associates on the ISO C++ Standards committee as well as two of the committee’s study groups: SG14 (low-latency) and SG20 (education). He has spoken at industry conferences, including the C++ and System Software Summit, the Embedded Systems Conference, NDC Techtown, and CppCon, where he’s also chair of the Embedded Track and a member of the program committee. Ben previously worked as a software engineer for Vorne Industries, where he used C++ and JavaScript to develop embedded systems that help improve manufacturing productivity in factories all over the world. He’s also a contributing author on multiple Vorne patents.
---
Videos Filmed & Edited by Bash Films: www.BashFilms.com
RU-vid Channel Managed by Digital Medium Ltd: events.digital-medium.co.uk
---
Registration for CppCon: cppcon.org/registration/
#cppcon #cppprogramming #cpp #initialization

Наука

Опубликовано:

 

18 янв 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 28   
@Fareoneo
@Fareoneo 6 месяцев назад
How beautiful is a language where you can make a one hour long video on initialization?
@johnnycashcow1130
@johnnycashcow1130 6 месяцев назад
There’s a whole book dedicated to initialization :-)
@ayushdey5494
@ayushdey5494 5 месяцев назад
​@@johnnycashcow1130 name Just curious
@matthijshebly
@matthijshebly 5 месяцев назад
C++ "beautiful"? Lol
@milad6914
@milad6914 5 месяцев назад
what's the name?@@johnnycashcow1130
@BenjaminBuch
@BenjaminBuch 4 месяца назад
I really hope that C++ Syntax 2 will leave the experimental stage before 2030 and get first citizen support in the most important tools. I love C++, but something as elementary as initialisation shouldn't be so complicated. A few really dark places in relation to templates didn't even appear here.
@siddheshkalekar5632
@siddheshkalekar5632 6 месяцев назад
Great talk !! Thanks for the historical explanation of probably the most confusing topic
@kimicochiang
@kimicochiang 6 месяцев назад
Thanks Ben! I really appreciated you and Dan gave me opportunity to get to know cpp more.😊
@trondenver5017
@trondenver5017 6 месяцев назад
Another Saks banger
@supershaye
@supershaye 6 месяцев назад
Perfect. I was just reading the C++ standard paper N1890 and N1919 to understand the initialisation story so this is a perfect recap!
@Evan490BC
@Evan490BC 4 месяца назад
Yes, the infamous C++ initialisation horror story.
@xwize
@xwize 6 месяцев назад
Excellent talk
@MyMjrox
@MyMjrox 5 месяцев назад
I cannot find the slide for it in the link to the repository?
@aviralvikram9431
@aviralvikram9431 Месяц назад
Hello, you shared some greate knowledge with us here and i want to save it in my stash collection but i can't find the ppt for this presentation the github repo of cppcon. Does anyone have any idea where can i find it?
@msmeraglia
@msmeraglia 6 месяцев назад
lol this thumbnail looks like something out of a Tim & Eric sketch
@msmeraglia
@msmeraglia 6 месяцев назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-O7KnURR7Vvk.html
@placintaalexandru855
@placintaalexandru855 Месяц назад
The more I look at C++ talks, the more I am grateful to Rust
@s0rr0wHacker
@s0rr0wHacker 6 месяцев назад
My team lead made me watch this, greetings to him
@v-for-victory
@v-for-victory 24 дня назад
🎯 Key points for quick navigation: 00:00 *🎓 Introduction to Initialization in C++* - Introduction to the session and the presenter, Ben Saks, - Overview of the complexity of initialization in C++, - Initial example showing different initializations depending on the type. 02:12 *📜 Historical Context and C Initialization Rules* - Explanation of C initialization rules for scalars and aggregates, - Copy initialization for structures and unions, - Zero initialization for static or thread-local storage duration objects. 06:10 *🚀 C++ Initialization Enhancements* - Introduction of class types and constructors in C++, - Differences in initialization rules due to class invariants, - Definition and restrictions of aggregates in C++03 and updates in C++20. 12:06 *⚙️ Direct vs Copy Initialization* - Direct initialization using constructors and member initializer lists, - Clarification on copy initialization using the equals sign, - Distinction between initialization and assignment. 18:08 *🏗️ Understanding Constructors and Copy Initialization* - Examples of initializing class objects and invoking constructors, - Handling class types with user-defined constructors, - Explanation of converting constructors and explicit constructors. 24:00 *🛠️ Default and Value Initialization* - Default initialization and the concept of vacuous initialization, - Parsing ambiguities and the most vexing parse issue, - Usage of empty parentheses in various contexts for value initialization. 30:00 *🔍 Deeper Dive into Value Initialization* - Behavior of value initialization based on the type T, - Value initialization for arrays and scalar objects, - Evolution of value initialization syntax from C++03 to C++17. 32:06 *🧩 Problems Addressed by Modern C++ Initialization* - Challenges with non-uniform initialization syntax, - Issues with narrowing conversions and complex value initialization, - Specific problems with initializing STL containers. 32:36 *💻 Challenges with Initialization in C++03* - Types and initialization forms: Scalars, aggregates, and class types with user-provided constructors, - Difficulty in knowing the right initialization form in templates, - Issues with silent narrowing conversions and initializing STL containers. 37:12 *🛠️ Modern C++ Initialization Improvements* - Introduction of brace initialization for uniform syntax, - Benefits like avoiding function parsing ambiguity and supporting value initialization, - Examples of using brace initialization with different types. 42:53 *🔍 Detailed Look at Direct and Copy List Initialization* - Explanation of direct list initialization and copy list initialization, - Differences in handling explicit constructors and narrowing conversions, - Examples illustrating when each form of initialization is used. 48:14 *🧩 Handling Narrowing Conversions and Initializer Lists* - Prohibiting narrowing conversions with brace initialization, - Using explicit casts to handle necessary narrowing conversions, - Creating and using initializer list constructors for flexibility in container initialization. 53:10 *🔄 Managing Constructors and Initializer Lists in Templates* - Impact of having multiple constructors, including initializer list constructors, in template classes, - Recommendations for carefully designing classes with initializer list constructors, - Example of potential confusion and best practices in handling initializer lists and template functions. 59:06 *📚 Best Practices and Template Initialization* - Using direct initialization in standard library functions like make_unique, - Handling aggregate initialization with C++20's improvements, - Conclusion with a summary of the evolution and benefits of modern C++ initialization rules.
@SEEJMAN
@SEEJMAN 3 месяца назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-_23qmZtDBxg.html At 44 minutes Ben states that "direct-list-initialization would permit conversions from const char* to demo_str". However clang 16 report an error. struct demo_str { explicit demo_str(const char*){} }; struct C { C(int, demo_str const&){} }; int main() { C c {1, "asdf"}; // error: no matching constructor for initialization of 'C' }
@sxxbxx2197
@sxxbxx2197 20 дней назад
The average programmer doesn't understand all the things that need to go on under the hood. All this stuff in C++ allows library writers to provide the under the hood stuff instead of it requiring everything to be provided by the core language teams.
@MuharremGorkem
@MuharremGorkem 6 месяцев назад
A question: Do you believe that C++ will ever save itself from C or even from itself to become a humanly programming language BEFORE LLMs will kill all programming languages including and hopefully starting from C++ ? By just watching this video alone you may easily see that complexity of initialization is comparable to whole instruction set of a CPU....So to save ourselves from writing assembly, we are supposed to write in C++ as a simplification?? How ironic.... Well, I admit that C++ would be perhaps a candidate for underlying-assembly language of LLMs in some near feature.
@marco21274
@marco21274 6 месяцев назад
Do you believe that currently LLMs are good enough? Maybe for some mobile app but for something more important? Many of the Rust programmers I met were hard core C programmers before. The had some really emotional dislike for C++. I see C++ as a pragmatical language. It is a tool. Can we improve the tool? Definitely! Can there be other tools who fits some jobs better. Yes too. But will be there a silver bullet? My experience says no. There were many proposed silver bullets. They needed the hype to find their niche but they had drawbacks too. Now the newest hype ist Rust. Will it be better for some problems? Definitely. Will it fix C++ biggest problems. I highly doubt it. People love to speak about resource management. But actually it is not nearly the biggest problem I encounter. C++ has already quite good tools for that. Especially compared to C.
@ryleitdept
@ryleitdept 6 месяцев назад
I still believe that direct machine code is hard and takes time to understand.LOLS
@AlfredoCorrea
@AlfredoCorrea 6 месяцев назад
I think static analysis is already saving C++ from C. They do what compiles can't do: breaking with residual C syntax and forcing (suggesting) one syntax over the 20 other alternatives from backward compatibility. but people have to voluntarily opt-in and set up build systems with this.
@Evan490BC
@Evan490BC 4 месяца назад
In a rare feat of proactive thinking the C++ committee designed the C++ programming language in a form that no LLM can generate it!
@tbkih
@tbkih 4 месяца назад
Please, please, next time look into the camera and say "it's free real estate"
@wallacesousuke1433
@wallacesousuke1433 Месяц назад
Programming SUX