- 502
- 8 038 862
C++ Weekly With Jason Turner
United States
Registrace 8. 09. 2007
Making C++ Fun and Accessible.
C++ Weekly - Ep 430 - How Short String Optimizations Work
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c
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/lefticus/cpp_weekly/issues/98
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-spring.com/
WANT MORE JASON?
► My Training Classes: emptycrate.com/training.html
► Follow me on twitter: lefticus
SUPPORT THE CHANNEL
► Patreon: www.patreon.com/lefticus
► Github Sponsors: github.com/sponsors/lefticus
► Paypal Donation: www.paypal.com/donate/?hosted_button_id=PQ4A2V6ZZFQEU
GET INVOLVED
► Video Idea List: github.com/lefticus/cpp_weekly/issues
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cpp23_best_practices/
► 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/objectlifetimepuzzlers_book1
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book2
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book3
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book1
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book2
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book3
► 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/cpp_starter_project
► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices/cppbestpractices
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/inheritance-and-polymorphism/9781491961933/
► Learning C++ Best Practices - www.oreilly.com/library/view/learning-c-best/9781491954898/
Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html
Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c
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/lefticus/cpp_weekly/issues/98
T-SHIRTS AVAILABLE!
► The best C++ T-Shirts anywhere! my-store-d16a2f.creator-spring.com/
WANT MORE JASON?
► My Training Classes: emptycrate.com/training.html
► Follow me on twitter: lefticus
SUPPORT THE CHANNEL
► Patreon: www.patreon.com/lefticus
► Github Sponsors: github.com/sponsors/lefticus
► Paypal Donation: www.paypal.com/donate/?hosted_button_id=PQ4A2V6ZZFQEU
GET INVOLVED
► Video Idea List: github.com/lefticus/cpp_weekly/issues
JASON'S BOOKS
► C++23 Best Practices
Leanpub Ebook: leanpub.com/cpp23_best_practices/
► 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/objectlifetimepuzzlers_book1
► Object Lifetime Puzzlers Book 2
Amazon Paperback: amzn.to/3whdUDU
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book2
► Object Lifetime Puzzlers Book 3
Leanpub Ebook: leanpub.com/objectlifetimepuzzlers_book3
► Copy and Reference Puzzlers Book 1
Amazon Paperback: amzn.to/3g7ZVb9
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book1
► Copy and Reference Puzzlers Book 2
Amazon Paperback: amzn.to/3X1LOIx
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book2
► Copy and Reference Puzzlers Book 3
Leanpub Ebook: leanpub.com/copyandreferencepuzzlers_book3
► 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/cpp_starter_project
► C++ Best Practices Forkable Coding Standards - github.com/cpp-best-practices/cppbestpractices
O'Reilly VIDEOS
► Inheritance and Polymorphism in C++ - www.oreilly.com/library/view/inheritance-and-polymorphism/9781491961933/
► Learning C++ Best Practices - www.oreilly.com/library/view/learning-c-best/9781491954898/
zhlédnutí: 7 997
Video
C++ Weekly - Ep 429 - C++26's Parameter Pack Indexing
zhlédnutí 7KPřed 19 hodinami
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/376 T-SHIRTS...
C++ Weekly - Ep 428 - C++23's Coroutine Support: std::generator
zhlédnutí 7KPřed 14 dny
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c CLion is a cross-platform JetBrains IDE for C and C with: - A smart...
C++ Weekly - Ep 427 - Simple Generators Without Coroutines
zhlédnutí 9KPřed 21 dnem
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/304 T-SHIRTS...
C++ Weekly - Ep 426 - Lambdas As State Machines
zhlédnutí 13KPřed 28 dny
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c CLion is a cross-platform JetBrains IDE for C and C with: - A smart...
C++ Weekly - Ep 425 - Using string_view, span, and Pointers Safely!
zhlédnutí 11KPřed měsícem
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 424 - .reset vs →reset()
zhlédnutí 10KPřed měsícem
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c CLion is a cross-platform JetBrains IDE for C and C with: - A smart...
C++ Weekly - Ep 423 - Complete Guide to Attributes Through C++23
zhlédnutí 9KPřed měsícem
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 422 - Moving from C++20 to C++23
zhlédnutí 9KPřed měsícem
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/359 T-SHIRTS...
C++ Weekly - Ep 421 - You're Using optional, variant, pair, tuple, any, and expected Wrong!
zhlédnutí 25KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
C++ Weekly - Ep 420 - Moving From C++17 to C++20 (More constexpr!)
zhlédnutí 8KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Episode details: github.com/lefticus/cpp_weekly/issues/359 T-SHIRTS...
C++ Weekly - Ep 419 - The Important Parts of C++23
zhlédnutí 13KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c This episode is sponsored by think-cell. think-cell.com/cppweekly E...
CS101++ - What Are Open Source and GitHub?
zhlédnutí 1,5KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
CS101++ - What is`goto`?
zhlédnutí 1,6KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c github.com/lefticus/cpp_weekly/discussions/categories/cs101 for ope...
C++ Weekly - Ep 418 - Moving From C++14 to C++17
zhlédnutí 7KPřed 2 měsíci
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟ Upcoming Workshop: Understanding Object Lifetime, C On Sea, July 2, 2024 ► cpponsea.uk/2024/sessions/understanding-object-lifetime-for-efficient-and-safer-cpp.html Upcoming Workshop: C Best Practices, NDC TechTown, Sept 9-10, 2024 ► ndctechtown.com/workshops/c-best-practices/4ceb8f7cf86c Create anything, find anything, fix anything, and navigate to anyth...
C++ Weekly - Ep 417 - Turbocharge Your Build With Mold?
zhlédnutí 6KPřed 3 měsíci
C Weekly - Ep 417 - Turbocharge Your Build With Mold?
CS101++ - What Are `if`/`else` Statements?
zhlédnutí 1,3KPřed 3 měsíci
CS101 - What Are `if`/`else` Statements?
C++ Weekly - Ep 416 - Moving From C++11 to C++14
zhlédnutí 6KPřed 3 měsíci
C Weekly - Ep 416 - Moving From C 11 to C 14
CS101++ - What Are Computability and the Halting Problem?
zhlédnutí 2,1KPřed 3 měsíci
CS101 - What Are Computability and the Halting Problem?
C++ Weekly - Ep 415 - Moving From C++98 to C++11
zhlédnutí 8KPřed 3 měsíci
C Weekly - Ep 415 - Moving From C 98 to C 11
CS101++ - Exploring Circuit Simulation Tools: Online & Offline
zhlédnutí 2,5KPřed 3 měsíci
CS101 - Exploring Circuit Simulation Tools: Online & Offline
C++ Weekly - Ep 414 - C++26's Placeholder Variables With No Name
zhlédnutí 11KPřed 3 měsíci
C Weekly - Ep 414 - C 26's Placeholder Variables With No Name
CS101++ - What Are Nibbles, Bits, Bytes and Words
zhlédnutí 2,3KPřed 3 měsíci
CS101 - What Are Nibbles, Bits, Bytes and Words
CS101++ - What Are Binary, Hexadecimal, Octal and Decimal?
zhlédnutí 1,5KPřed 3 měsíci
CS101 - What Are Binary, Hexadecimal, Octal and Decimal?
C++ Weekly - Ep 413 - (2x Faster!) What are Unity Builds (And How They Help)
zhlédnutí 7KPřed 3 měsíci
C Weekly - Ep 413 - (2x Faster!) What are Unity Builds (And How They Help)
I think I would have use the capacity has the common field instead of the size because if for example you modify the length of your string a lot between let say 12 and 20 characters your solution force you to allocate deallocate each time you cross the 16 byte boundary. Whereas the capacity can stay at ~20 while your size can move around as it want. To use capacity instead we can say that size() = capacity when capacity < 16 else size and capacity() = max(16,capacity)
I compared aarch64 binaries with x86 abidiff, it just empty.
awesome one!
(++i)++;
honestly I'd rather if small string was a different type, maybe something like: small_string<10> str;
I don't know why they didn't choose to do that in the first place. Just let the user specify the size of the small buffer based on their needs. Now we're limited to a 16 byte buffer.
Isn’t std::size_t in <cstddef> not <cstdint>?
Yeah
It is a shame that we had to wait 26-11=15 years for this feature
To pack this object into something even smaller in space you could use a bit field for the m_size, and while you can't allocate a string that is unsigned int64, you can allocate a unsigned int63. And use a bit at either end to indicate whether the space was allocated on the heap or using your m_small field of your other union. There of course is a minimal overhead of looking at those bit fields vs a bool or the int, but this is a thing that maps doing red/black do.
Is std::launder necessary for unions? I know the answer is no when the data members are active only once in the lifetime of the union, but this is not the case here.
A good exercise is implementing small object optimised vector<bool>
Or just (ab)use std::basic_string<> for arrays with small object size optimizations.
I would have made `is_small_storage()` a function which returns either `m_size < ` an arbitrary constant or the result of `m_size < sizeof(small_storage)` (so no additional storage needed for the bool). In several functions we would then need to re-evaluate this but I think it's worth it as it takes less space.
Also, a "problem" with this and the video version is that the assignment operator would need to deallocate the memory if you assign a small string to a previously large string, because you would loose the pointer location
@@antonpieper Yes the check has to be done for any of the modifying operations I guess.
I think the real optimization would be just to make the most significant bit of the size a flag
@@Mozartenhimer Doesnt that reduce the max size of the container? It's an undefined integer type so all the bits are used.
@@mjKlaim I don't think you'll miss that 9.2 exabytes.
Would love to see a std::variant version
btw, offtopic question: since you really shouldn't store a function address in a void pointer, it's recommended to use a dummy function pointer type instead. The question is: can you reliably store a method pointer into this dummy function pointer type? Or what can you use instead of a void pointer to store a method pointer? 🤔
constexpr is essentially a compile-time UB sanitizer😮
Why Boolean needed to check if it is a small object? Size is present. Max size of small object also is known. I doubt that checking Boolean is much faster then checking if some value is less then a constant…
Well, if you shrink the heap allocated string you don't reallocate so that won't work but there are better ways to do this.
Raymond Chen has written a great post (Inside STL: The string) about this.
If the string is shrunk, ie a short string is put into a what was a long string, you'd have to be sure to deallocate the extra space. That might be surprising to some users. Ie, long string, ... allocate short string ... deallocate long string ... allocate again. Vs long string .. allocate short string, .... no deallocation long but not too long string ... also no reallocation
size can be zero even if it's a heap-allocated string. But capacity does reflect it's nature tho, you can use that, as long as heap allocated capacity is always larger than the max small string capacity
I've just googled this topic today! PS: one could pack the small string even tighter, there's a way to use single byte for a size, but you'd store the max_small_string_capacity minus size instead. When the small string size equals the max capacity, the capacity minus size becomes zero and acts as a null terminator. 🤯
That sounds like UB, given that the compiler is free to add padding to its liking.
@@higaski This trick that @user-cy1m5vb71 uses, could be put into yet another union, and thus you'd know about the packing.
struct string { struct s_large { char* data; // assuming it's a 64bit pointer uint64_t size; // maybe even some padding to add extra stack space // like char padding[8]; }; struct s_small { static constexpr max_capacity = sizeof(s_large); char data[sizeof(heap) - 1]; char max_capacity_minus_size; }; uint64_t capacity; union { s_large l; s_small s; } data; }; even if there's some alignment padding added to the members of the s_large, the s_small should still be fine
@@user-cy1rm5vb7i I don’t get this approach. Won’t requesting c_str for a small string not be guaranteed-null-terminated?
@@higaskiit's not UB
I think this has to be one of the coolest examples on how and why to use constexpr.
Yep, i've been writing my own custom string class implementation and i had to debug such cases at runtime which take me quite some time to make it right, whereas constexpr simply allow us to see the problem at compile time, which is very useful.
Are there any books on c++26 yet?
Instead of hiring Jason Turner for onsite training, you could also buy the book on move semantics by Nicolai Josuttis (probably spelled the name wrong). It covers all dark corners of move semantics in detail and I guess it is considerably cheaper. The usecase of std::move is that if the member data of Lifetime is not an int, but a pointer to dynamically allocated memory, std::move(l1) will cause the move constructor to be called instead of the copy constructor. The copy constructor will do a dynamic allocation and copy the data, the move constructor will simply copy the pointer. You will still have 2 objects (one moved from, one moved to) but not an allocation and not a memcpy. That is the benefit of std::move. In the case he demonstrated here, you don't want move semantics, because that won't help you here. Here you want in-place construction, emplace() and the like. The lambda creates an object, elides the copy of the return value and so avoids the construction of a temporary. It's effectively an emplace.
This is great because it has the potential to decrease compile times significantly.
With expected is even worse! I'm using a static std::expected<Class, Err> to construct my object, and having a private constructor, but this blocks me from passing std::in_place + Args, as the constructor is private... How can I apply rvo in this case? will "return std::expected<Class, Err>(Class(args)); " work? I tried this and it called the move ctor...
C++ will never die, because we all love it so much!
Can we thank circle for this?
Careful tho, this implementation technically returns you an input_range, not a forwarding one, since the sequence is technically single pass only. The resulting view doesn't reflect that tho, algorithms you pass this into might break
❤
On that note You can also do Param...[2] To get the type at index 2
Can you also index into the types of the parameter pack and make some compile time checks there? E.g. std::is_same_v<int, Param...[3]> ?
yes. args...[3] is the value and Args...[3] is the type and can be used with <type_traits>.
This is really cool, but i dont see it being very useful in well designed code.
I would guess that most people, even if they want to use that, they will not be moving their codebase to C++26 until C++29 or later are released, because companies don't usually make this type of jump as freely...
Even with C++23 finished, because clang and gcc haven't finished their module implementations, (and iirc clang's concepts [least on Mac] can still produce ICEs, I know at least Apple's clang still hates some requires clauses, they simply don't work) most folks are still stuck on C++17 for universal support. I really hope the delay in finishing modules and properly functioning concepts doesn't delay development and adoption.
FINALLY!
Одна из главных вещей, которые я ожидаю в c++26
What happens if the parameter pack has only two elements and you try to access the 3rd elelement? Is it a no-op? Is it a compiler error? Can you test for at compile time so you enable a different code path in the template body?
if constexpr (sizeof...(T) > some_constant_expression) { // do your thing }
@@testtest-qm7cj Thanks. So I assume from your answer: a) That there will be a compile time error, b) You can check for it in compile time and decide a different code path.
@@sledgex9 a) yes, a compile time error is issued. b) yes, either you do what I showed above in the function body, or add another "requires-clause" to help your compiler. For example, extending Jason's code in the video, "requires (count>0) && ( count <= sizeof...(Param) )" would generate much nicer compile time error message when you try to access an out-of-bound element. By the way, "sizeof...(T)" is C++11, "if contexpr()" is C++17, and "requires ()" is C++20. You can test all these today.
@@testtest-qm7cj oh, so the only thing missing was a way to index into an item directly, which they will add in C++20.
I think you can also unpack into a tuple and then use get<n>(tuple) to access it yeah?
Amazing innovation, now I write it with my hands and cunning overload operators.
very cool
Does C++26 have ranges parameter pack support?
that params...[index] syntax is ugly. C++ honestly needs a new fresh coat of paint with better defaults, it's a beautiful and powerful language. just a bit ugly
well, is it beautiful or ugly?
It makes sense to me. It looks good.
auto p = co_get<2>(params...);
If cppfront become production ready and catch on...
it makes perfect sense given that we use ... for varadic behavior already, and that was a C thing first, and some other languages do that.
Considering that variadic templates came at C++14 (I think, haven't checked), one would assume this should have been added years ago.
Better late than never, but yes, very much so.
yeah, made me mad every time because the compiler has the list, but lets you not use it
Variadic templates was a C++11 feature. I don't think C++14 did anything to even mess with them.
C++26 will be great. I wonder how long it takes for it to be deployed.
As stated, in the video, it's already in Clang (but not in release afaik)
@@gregthemadmonkThis feature is in clang. Not necessarily further fratures of the nee standard.
For C++26 to actually be mostly fully implemented will probably at least take until mid to late 2027. The thing currently that looks like it might be the most complex is reflection
Very very nice... Is there a way of checking if the third parameter exists, though? What would happen if the third parameter doesn't exist? Compile time error, warning, UB??
Most probably a compile time error because the object doesn't exist
@@unluckyaf7673 You can always use sizeof...(T) to test, which is a C++11 feature.
I just tried it. It gives a really nice error: Error: invalid index 3 for pack params of size 3 return params...[3]; And it has an arrow to the "params" Then it also give stage a "note": note: in instantiation of function template specialization "func<int, const char*, int> requested here: auto var = func(1,"test", 2);
Good, Now we need p1061 and P3115 and maybe P2994 and then maybe, just maybe, my hair will thank me for stopping puling it.
yeah!..... for the longest time..
It feels like the input of a matlab function, but with the zero abstraction cost of c++. Awesome.
Hey look, something with compile time bounds checking :D
Nice one :p
Constexpr context already gives you that (and more) guarantee(s).
Well, another way would be to write this as ``` template<typename... Param> void func(Param... param) { std::get<2>(std::tie(param...)) = 42; } ```
Wouldn't that allocate a large structure to store the references for the std::tie-object. params...[2] is probably far more efficient
@@BaardFigur Clang optimizes it away. The outputs are identical.
@@X_Baron And this is why we don't worry about things like this until they _need_ optimization. The compiler is usually already able to write better code than you will most of the time, even more so if you help it out with good basic practices like liberal use of const(expr). Be aware of things like C++ being row-major instead of column-major, but don't waste time frobnicating every bit of code until you've profiled it.
great
One of the other ways you could do it prior to C++26 is by using std::tie to get references to the parameters and then using std::get<i>(tuple).
Or std::forward_as_tuple if you need to ref qualifiers
at 8:00 wouldn't return std::string(b?"Hello":"World"); also work? and it would be easier to read/write I think.
to avoid padded warnings you can introduce "char padding" members in your struct, to fill the gaps. this also allow for more easy knowing of were you can put new members in this struct wo breaking the alignment
Thanks for the heads up...problem solved