Тёмный

C++ Weekly - Ep 312 - Stop Using `constexpr` (And Use This Instead!) 

C++ Weekly With Jason Turner
Подписаться 110 тыс.
Просмотров 50 тыс.
50% 1

☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟
Upcoming Workshop: Understanding Object Lifetime, C++ On Sea, July 2, 2024
► cpponsea.uk/2024/sessions/und...
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/
I'm going to regret this title in the future, aren't I?
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/...

Наука

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

 

20 фев 2022

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 123   
@cppweekly
@cppweekly 2 года назад
TL;DR, we are talking about constant expressions, and this is the only specific guarantee about constant expressions and when they are evaluated that I can find, from [expr.const]: "[Note: Constant expressions can be evaluated during translation. - end note]" (can, not shall or must) ------- I have received some off-RU-vid comments questioning the validity of this episode. So here is a breakdown of the standard, plus some notes from Herb Sutter at the end. ------ First, let's look at what `constexpr` means, when applied to a variable, according to the standard: eel.is/c++draft/dcl.constexpr#10 [dcl.constexpr] A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. In any constexpr variable declaration, the full-expression of the initialization shall be a constant expression ([expr.const]). A constexpr variable shall have constant destruction. ------ It is const, and it is a "constant expression." No other guarantee is made. Important: constant expressions are not values computed at compile time. They are values that meet a specific set of requirements. From the standard: [expr.const] (eel.is/c++draft/expr.const#1) Certain contexts require expressions that satisfy additional requirements as detailed in this subclause; other contexts have different semantics depending on whether or not an expression satisfies these requirements. Expressions that satisfy these requirements, assuming that copy elision (11.10.5) is not performed, are called constant expressions. [Note: Constant expressions can be evaluated during translation. - end note] Note this statement "can be evaluated during translation." "Translation" is what we think of as "compilation" (eel.is/c++draft/lex.phases) This means there is no requirement at all when the values are computed, as long as it meets the requirements. ------ Next, let's look at the term "potentially-constant" eel.is/c++draft/expr.const#4 A variable is potentially-constant if it is constexpr or it has reference or const-qualified integral or enumeration type. A constant-initialized potentially-constant variable is usable in constant expressions at a point P if its initializing declaration D is reachable from P and -(4.1) it is constexpr, (there are many subclauses here, and it keeps going as you dig deeper into "potentially constant evaluated" "immediate invocation" "manifestly constant-evaluated" (which determines if "is_constant_evaluated" returns true) and "needed for constant evaluation"...) ------ So let's come back around to how `static` changes the situation: eel.is/c++draft/basic.start.static#2 [basic.start.static] Constant initialization is performed if a variable or temporary object with static or thread storage duration is constant-initialized ([expr.const]). If constant initialization is not performed, a variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is zero-initialized ([dcl.init]). Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. All static initialization strongly happens before ([intro.races]) any dynamic initialization. So, we are aiming for "constant initialization" which is a form of "static initialization." We need a "constant-initialized" "static" variable... ------- Phew, ok, that's what we got with `static constexpr` `constinit` was added in C++20 to also force this "static initialization" (the only form of initialization guaranteed to happen at compile-time). www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1143r2.html. Every initialization that is not "static initialization" is "dynamic initialization", which is not guaranteed to happen at compile-time. ([basic.start.static] comes up again). From what we can read, the only guarantee about a "constexpr" function or variable, is that it's "useable as a constant expression." -------- And here is the wording from Herb (isocpp.org/blog/2013/01/when-does-a-constexpr-function-get-evaluated-at-compile-time-stackoverflow) "Trying to summarize: constexpr isn't about forcing execution to happen at compile time always. It's about enabling programmers to more easily write functions and variables that, in addition to their normal uses, are also guaranteed to be legally usable where the language requires compile- time "constant expressions" (e.g., to initialize enumerator values and array bounds). The keyword "constexpr" is intended to connote that: "can be used in constant expressions." Granted, "constant expressions" is a precise standardese term, so we're always open to better ways of teaching this."
@switchblade6226
@switchblade6226 2 года назад
A good way to think about constant expressions is that `constexpr` is a hint that lets the compiler know that the variable or function *CAN* be evaluated at compile time, but it is not required (unless you, explicitly require it via constinit or NTTP or whatever). And another way to think about it is that the `value` of constexpr is calculated, but not the location. Kind of like using a regular literal, the compiler knows that there is a known value, but it will still need to copy it (or it's address) if you require so. constexpr controls the value, but not the location, when you set it to static you say that you want to have only one instance of that constant, thus the compiler can do more optimizations.
@thomassynths
@thomassynths 2 года назад
This is the real answer.
@Demonslay335
@Demonslay335 2 месяца назад
@@thomassynths As usual, almost every keyword in C++ is just a HINT to the compiler of intention, it will always do it's own thing in the end.
@hymen0callis
@hymen0callis 2 года назад
C++ never fails to surprise you... Great video
@uvuvwevwevweossaswithglasses
Because its Sea(C) underneath
@kortaffel
@kortaffel 2 года назад
2018: Yo dawgs, use constexpr 2019: Guys, constexpr is cool 2020: Have you all heard of constexpr? 2021: Constexpr all the things 2022: Stop using `constexpr` Okay ... whatever you say, boss
@tcocaine
@tcocaine 2 года назад
nice meme
@cppweekly
@cppweekly 2 года назад
constexpr all the things was 2017. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-HMB9oXFobJc.html
@ThePlacehole
@ThePlacehole 2 года назад
Maybe every single keyword in c++ can be a dangerous trap eventually.
@Nobody1707
@Nobody1707 2 года назад
Which keyword isn't already a dangerous trap?
@ezekieloruven
@ezekieloruven 7 месяцев назад
​@@Nobody1707 even things like "int" can be dangerous if you know nothing about signed overflow undefined behavior.
@peregrin71
@peregrin71 2 года назад
And there I was expecting "consteval"... still good feedback on the use of static. Thanks
@kodref
@kodref 2 года назад
I was too.. And I've been told before static variables are evil.. Now what to think?
@carlpittenger
@carlpittenger 8 месяцев назад
​@@kodref non-const statics should be avoided in most situations, but there is nothing wrong with const statics
@carlpittenger
@carlpittenger Месяц назад
@@anon8510 mutable global state is notoriously error-prone
@treyquattro
@treyquattro 2 года назад
this was indeed interesting. I think most C++ programmers know that constexpr doesn't connote a storage duration, but probably most like me were not as familiar with the standard as perhaps we should be to know to use this work-around to force constant evaluation, if possible (one way or the other - with or without optimization). To my mind it does show that C++ is more complicated than it really ought to be, and it shows that you can't assume that all compilers will generate code in even a similar way. It's a good idea to compile with address sanitization switched on if you can - I've been doing it for quite a while already and it has saved me a couple of times.
@cppweekly
@cppweekly 2 года назад
> I think most C++ programmers know that constexpr doesn't connote a storage duration it is a common misconception though, and in general even people who understand that don't understand that you are still asking the compiler to put things on the local stack.
@Phroggster
@Phroggster 2 года назад
What's sad is I've been aware of this "one neat trick" for some time, but it never seems to actually register in my head while the rough drafts are getting inked. This video just helped me to correct my ways.
@Evan490BC
@Evan490BC 2 года назад
This is normal with C++. It's so huge it's like trying to memorise the whole of Homer's Odyssey by heart....
@TibRib
@TibRib Год назад
Compiler explorer probably has additional guards that are counter intuitive, it will not tell you about stack overflows or other exceptions; It's important to keep this in mind when prototyping
@von_nobody
@von_nobody 2 года назад
Isn't this other way around? `constexpr` objects are real `const` objects, and you use address of it it will require to "materialize" object, like `constexpr int i = 3; foo(&i);` will cause to create `i` as stack variable.
@protheu5
@protheu5 11 месяцев назад
Thanks, now I will pay more attention to constexpr.
@dagbruck
@dagbruck 2 года назад
The one point I’m missing: when would the compiler do things differently is you used const instead of constexpr?
@DegradationDomain_stuff
@DegradationDomain_stuff 2 года назад
Jason 2021: Use constexpr! Jason 2022: Don't use constexpr!
@dmitryincog7455
@dmitryincog7455 2 года назад
C++ is such controlled flight into terrain. Can't speak for everyone, but all programmers that I know of just want robust compile-time execution - not this fanciful constructs. Jason is so secure in his job explaining to us how nonsensical C++ is.
@sirnawaz
@sirnawaz 2 года назад
You don't have much options either in domains where C++ is used. Rust is one option, but it isn't as close as C++ is. Also, as Rust is trying to be as good as C++ is, it's also becoming complex with each new addition!
@slyfox3333
@slyfox3333 7 месяцев назад
​@@sirnawazthings can be complex without being nonsensical like c++
@yash1152
@yash1152 Год назад
> _"Oh OK, going for Clickbait today?"_ thanks a lot for mentioning that in the thumbnail
@fcolecumberri
@fcolecumberri 2 года назад
Is there any case where you prefer `constexpr` over `static constexpr`?
@carlpittenger
@carlpittenger 8 месяцев назад
my thoughts exactly; idk why they didn't make static the default...
@michaelmahn4373
@michaelmahn4373 2 года назад
Unfortunately you can't declare a variable as static within a constexpr function. This doesn't compile: constexpr int getVal(std::size_t in) { static std::array constexpr arr{0, 2, 4}; return arr[in]; }
@cppweekly
@cppweekly 2 года назад
Stay tuned for next week. I found a workaround for that. Next week's episode is very important.
@weekendwarrior3420
@weekendwarrior3420 Год назад
12:00 I've heard before that, when we code undefined behavior, optimizer developers take the liberty to do what's easy and use the "undefined behavior" excuse when someone complains. This "out of scope" variable usage looks like a very crisp example of that.
@dennisrkb
@dennisrkb 2 года назад
Dear Jason, I immensely enjoyed your talk on constexpr, truly eye-opening. Are you aware of any fully constexpr unit testing framework?
@cppweekly
@cppweekly 2 года назад
you want the STATIC_REQUIRE capability that catch2 has. I use that in my C++ Starter Project github.com/cpp-best-practices/cpp_boilerplate_project/blob/main/test/constexpr_tests.cpp In the CMakeLists I show how you want to compile the tests twice. Once for runtime, once for constexpr usage: github.com/cpp-best-practices/cpp_boilerplate_project/blob/main/test/CMakeLists.txt#L21-L54
@psychotherapeuticprogrammi4619
@psychotherapeuticprogrammi4619 2 года назад
What would you suggest in case of a need for a static constexpr variable in a constexpr function? (considering that the constexpr function may in fact be called at runtime)
@cppweekly
@cppweekly 2 года назад
GREAT question!! Remember how I said I needed this episode because it's background for next weeks episode? Stay tuned for next week!
@turanmuratguvenc9106
@turanmuratguvenc9106 2 года назад
great one.
@willofirony
@willofirony 2 года назад
Who would have thought high level languages can make more sense when you see the assembly? The title is fine.
@mocloun3709
@mocloun3709 2 года назад
Your example (declaring a variable within a narrow scope and pointing to it) is either academic or a case of strong code smell and has to be avoided. If you declare it static you can declare it out of the narrow scope anyway. And then you don't need static.
@cppweekly
@cppweekly 2 года назад
My example existed solely to point out that `constexpr` does not affect object lifetime. If you have a constexpr variable you are asking the compiler to copy data into the local stack, and it will often oblige. Sometimes the copy is eliminated by the optimizer
@salimp2009
@salimp2009 2 года назад
This was really awesome ..how about making a consteval function that returns a compile time value ?
@cppweekly
@cppweekly 2 года назад
we'll cover that in 2 episodes
@salimp2009
@salimp2009 2 года назад
awesome 👍👍👏👏
@Nobody1707
@Nobody1707 2 года назад
One interesting wrinkle, `constexpr static` member variables are implicitly inline, but non-member `constexpr static` are not implicitly inline.
@cppweekly
@cppweekly 2 года назад
And that is a huge help in many cases.
@DavidDightOriginal
@DavidDightOriginal 2 года назад
Compilers implement block scope static variables with some sort of atomic mutex. Are there any performance costs associated with these?
@marselogklasman7068
@marselogklasman7068 Месяц назад
Good question. They should not as it’s initialised at compile time. No need all the lazy init checks and overhead.
@richardblain4783
@richardblain4783 Год назад
What about constexpr values and functions used in template arguments?
@ChaotikmindSrc
@ChaotikmindSrc 2 года назад
i don't see the point, the problem here is referring to something in a temporary scope, you did think about it too much IMO... you don't even need such a "complicated" setup to have the same results.
@cppweekly
@cppweekly 2 года назад
The point is that most people expect constexpr values to be static. they aren't. The rest of the people expect constexpr values to be computed at compile time... they aren't *required* to be. constexpr static solves both of those confusions.
@ChaotikmindSrc
@ChaotikmindSrc 2 года назад
@@cppweekly Thx for the added explanation, but as you said in the beginning of the video some of us would find that kinda obvious (and by far i'm not a c++ guru) Personally, the thing that really was painful to me, is that the introduced constexpr keyword, was not a forced and absolute compile time stuff, running ANY arbitrary code, and able to build structure/array/object at compile time That's the constexpr i needed !!!! The constexpr we have now is somewhat useless since advanced compilers could have done it already ?
@dejaniv
@dejaniv 2 года назад
@@ChaotikmindSrc That's an excellent point I was wondering myself. I still don't understand the difference between const and constexpr when used in constant declaration. My understanding is that constexpr in function declaration makes the compiler generate the result of the function during compile time and will make compiler error out if that's not possible which all makes sense to me. It's the use in constant declaration that escapes me. And btw. wouldn't static const have the same effect as static constexpr?
@ChaotikmindSrc
@ChaotikmindSrc 2 года назад
@@dejaniv constexpr is a "maybe" , not an obligation for the compiler, as Jason pointed many times static const will certainly result at least to some read only memory allocation at compile time, the mémory really exist at that address (bu t i'm unsure, the compiler can probably optimize that away) static constexpr can have the exact same effect i think but in practice, you get a bit more guarantee that it is optimized away, because you express the intent to the compiler. Also for functions, consider the case where it is obviously un optimizable ! I'll repeat myself : it is a bit frustrating IMO.
@marianrosu5023
@marianrosu5023 2 года назад
Thanks
@SuperBorisl
@SuperBorisl Год назад
I just need to ask for explanation here, if we declared constexpr variable, isnt initialisation of, guaranteed to be executed in compile time?
@cppweekly
@cppweekly Год назад
Technically not required unless you use it in a constexpr context. But it's reasonable to assume it will be calculated at compile time if assigned to a constexpr variable.
@sauce729
@sauce729 2 года назад
Would moving all constexpr variables to static cause slower performance at start up?
@zoriiginalx7544
@zoriiginalx7544 Год назад
Isn't it just baked into the binary though? If anything I think it would be faster because there's no stack allocation.
@mocloun3709
@mocloun3709 2 года назад
Why not declare all const instead of constexpr and let the compiler decide what to do? That's why we have the -O3 option.
@mafiabar6515
@mafiabar6515 2 года назад
if you want to force the optimizer to evaluate at compile-time just use "consteval", don't use static_assert()
@cppweekly
@cppweekly 2 года назад
Assuming you have C++20 and your function is consteval. Not all functions are, or should be, consteval. I cover that in 2 episodes.
@mafiabar6515
@mafiabar6515 2 года назад
@@cppweekly ofc, that's reasonable. but I was just talking about forcing optimizer to evaluate at compile-time and the answer is consteval not static_assert()
@nlhans1990
@nlhans1990 2 года назад
I also wonder if lots of people want to use consteval instead of constexpr, even if it's initialized to a static variable. Consteval ensures that the value is computed at compile time, while constexpr can still take non-const variables and give a "constant expression" for calculation, but not a compile time calculation. For example, a constexpr can still use a non-const variable for control flow. Then there is hardly anything constexpr (in terms of run time complexity) on it, it's just normal code that maybe can be evaluated at compile time.
@cppweekly
@cppweekly 2 года назад
in two weeks (314) I address this issue.
@spencers0730
@spencers0730 2 года назад
or you can leave all your functions constexpr (for flexibility) and then if you _really_ want something done at compile time, you can stick it in a consteval function (at the highest level) :)
@NickEnchev
@NickEnchev 9 месяцев назад
4:49 this is what irritates me about constexpr, you say: "We haven't done anythign to require....", except of course that get_value() and the variable in main are both marked constexpr explicitly!
@muitxer
@muitxer 2 года назад
what about consteval?
@cppweekly
@cppweekly 2 года назад
Stay tuned for 2 weeks from now. Ep 314
@Evan490BC
@Evan490BC 2 года назад
I think the keyword here is "can": constexpr variables _can_ be initialised at compile time, it's not a strict contract. Isn't that what constinit is for?
@cppweekly
@cppweekly 2 года назад
constinit is required to be static. Stay tuned for episode 314
@Evan490BC
@Evan490BC 2 года назад
@@cppweekly Yes, you're absolutely right (of course...). Just checked cppreference. Waiting for episode 314.
@devluz
@devluz 2 года назад
what does the -fsantize=address actually do and why not always use it?
@apenasmeucanal5984
@apenasmeucanal5984 2 года назад
afaik it makes compilation somewhat slower, but may help you catch a few bugs, so it’s more used when you suspect there’s something going on with your code.
@amaiorano
@amaiorano 2 года назад
The compiler injects a whole load of runtime checks for stuff like use-after-free. It adds about 10% overhead on normal programs.
@NagiTangCheong
@NagiTangCheong 8 месяцев назад
which pdf reader does you use
@cppweekly
@cppweekly 8 месяцев назад
Usually just Microsoft Edge. It's actually really good to use with pen interfaces too.
@Tyranisaur
@Tyranisaur 2 года назад
I've heard the counter argument that locals are more likely to be optimized away entirely compared to statics.
@cppweekly
@cppweekly 2 года назад
Probably from me, but `constexpr` changes the nature of the game a bit here too.
@YourCRTube
@YourCRTube Год назад
No mention about constinit and consteval?
@cppweekly
@cppweekly Год назад
They are irrelevant to this video. This video is about constexpr vs static constexpr. consteval is applied to functions, and constinit requires static already.
@petermuller608
@petermuller608 2 года назад
This is a great reminder of the difference between static constexpr and constexpr. ~~Nevertheless, static constexpr increases program startup time, thus sometimes you might prefer constexpr~~ Edit: I mixed up static with static constexpr
@cppweekly
@cppweekly 2 года назад
What do you mean, exactly, by "program startup time"? it does increase the size of the binary, which might increase load time, but it does 0 work at load time.
@petermuller608
@petermuller608 2 года назад
@@cppweekly you are right, I mixed up static constexpr with static with will run constructors before main
@marco21274
@marco21274 2 года назад
Actually is that the same as constinit const?
@cppweekly
@cppweekly 2 года назад
constinit is required to be static. So constinit x = some_func(); // is not valid static constinit const x = some_func(); is just a really expressive way of spelling: static constexpr x = some_func();
@jthrush
@jthrush 2 года назад
I have never used constexpr functions for the simple reason that if the calculation is too onerous to perform at run-time, I don’t want it to increase my compile time on _every compile_ either. My time is just as valuable as my customers. I precompute the values in a script or other short program and include them as a table of some sort. I’m sure this is an unpopular opinion.
@amaiorano
@amaiorano 2 года назад
It's not unpopular. Compile times are a real problem in C++, and not helped by all the compile time programming we do more and more (templates, constexpr). Some of this cost is alleviated by C++20 modules, thankfully.
@PaowZ
@PaowZ 2 года назад
Is it the same if you use ccache ?
@szaszm_
@szaszm_ 2 года назад
To the point that you need to run both Release and Debug builds: If you're building with both GCC and Clang, and don't want 2x2 jobs for just one OS, GCC gives more diagnostics at higher optimization levels, because some diagnostics come up during optimization. I'm not sure this happens with Clang, and it happened to show more diagnostics at no optimization in the video. For this reason, I would run GCC in Release mode and Clang in Debug. from the GCC docs, "Warning Options": > The effectiveness of some warnings depends on optimizations also being enabled. For example -Wsuggest-final-types is more effective with link-time optimization and some instances of other warnings may not be issued at all unless optimization is enabled. While optimization in general improves the efficacy of control and data flow sensitive warnings, in some cases it may also cause false positives.
@zdspider6778
@zdspider6778 7 месяцев назад
14:49 What do you mean "You almost always mean 'static constexpr' "? I don't understand... I don't want a static variable (a variable that lives until the program terminates). I want a variable evaluated at compile time, which is what 'constexpr' provides. For variables. For functions, "constexpr" is more of a hint to the compiler. If you really want to enforce compile-time evaluation for a function you use 'consteval' (introduced in C++20). 16:49-17:00 That's why you mark it as 'constinit' (introduced in C++20). Then yes... it needs to be static.
@cppweekly
@cppweekly 6 месяцев назад
You very very rarely mean "I want this to be evaluated at compile time (constexpr) and I want a copy of it to live on the current stack (non-static)" You generally mean "I want this to be evaluated at compile time, and I don't care where it lives" Non-static can be a real performance sink if you have a large constexpr object that has to be copied unto the stack every time a function is called. I have measured this impact in real code.
@technicalmachine1671
@technicalmachine1671 2 года назад
Title: C++ in a nutshell
@augustomafra3461
@augustomafra3461 2 года назад
On gcc -O0 copying the array for "constexpr auto values = get_values()", could we expect the compiler would actually do return value optimization? I understand that the best solution here is to use static constexpr, I was just wondering if constexpr could have some other interaction with RVO.
@cppweekly
@cppweekly 2 года назад
RVO tends to happen regardless of optimization level, because it is part of the ABI.
@__hannibaalbarca__
@__hannibaalbarca__ Год назад
I used it very … very rare. Reasons i still not learning about compile time calculations… i use only template function as meta programming… .I still like old fashion programming and is time to change. .very Good presentation.
@dagoberttrump9290
@dagoberttrump9290 Год назад
I found constexpr daunting first, but its actually quite simple: it just puts values into the binary. If you're using that value at some point during program execution, the instructions will still have to copy it in place
@metal571
@metal571 2 года назад
*static* constexpr all the things, 100%. It's also clear that higher optimization levels force compilers to do more analysis and can generate more warnings. Compiling a codebase only in debug mode (without optimization) can miss some critical warnings (which you are hopefully treating as errors with -Werror).
@LogicEu
@LogicEu 2 года назад
Hahahahahaha loved the sarcastic clickbait title!
@petermuller608
@petermuller608 2 года назад
Oh of course I knew exactly what constexpr did this entire time! ;)
@HamzaHajeir
@HamzaHajeir 9 месяцев назад
Use macros :)
@vvajay
@vvajay Год назад
Why would I use a pointer on a local array that is returned by a constexpr function? This doesn't make sense. You are over complicating things. Replace #define and const with (non-static) constexpr (for simple scalar values), that's all - a lot of legacy as well as new code is still using #define and consts, and not constexpr, and you're discouraging them to use constexpr with this complex and weird example?
@cppweekly
@cppweekly Год назад
You seem to have not actually finished the video. The point is that you should prefer `static constexpr` over `constexpr`.
@vvajay
@vvajay Год назад
@@cppweekly I did watch the entire video, and that's why I'm saying this. You're suggesting to fix something which isn't broken. It seems more like a dangling pointer issue, and not a constexpr issue.
@xle6ywek345
@xle6ywek345 2 года назад
Cool video, but my eyes are literally burning, could you turn on dark theme?
@cppweekly
@cppweekly 2 года назад
I've been sorting out how to get a decent looking dark theme in CE. I think I finally have what I want.
@_capu
@_capu Год назад
just write "and use static constexpr instead" in the title
@thomassynths
@thomassynths 2 года назад
TLDR: Do not blindly apply `constexpr` to your code without giving each variable or function definition some thought. The real reason to stop using `constexpr` is because it is a parasitic feature that forces your API to be publicly implemented. This is a big "Yikes!!!" because you cannot later un-`constexpr`ify your API without breaking client code. (Not to mention sinful compile times of header-only libraries and other real-world requirements, such as not wanting to publish source code.) I'm all down for templates and such, but as a strong rule-of-thumb I opt into them for semantic power, not for optimization reasons. I apply these exact same principles to non-local non-private `constexpr` declarations. 90% of the time I only declare something `constexpr` if I have to pass the value as an argument to a template or something like that.
@idldmit
@idldmit 2 года назад
I think it should be called "Stop passing pointers of temporary variables outside of its scope"
@cppweekly
@cppweekly 2 года назад
That was only just a tiny part of the point of this video. I needed to illustrate that constexpr does not affect object lifetime.
@ryannicholl8661
@ryannicholl8661 Год назад
This video is wrong. That value isn't a "stack value", it's an "automatic storage variable". This kind of terminology is harmful because it teaches people to use assembly reasoning on C++ code, instead of explicitly teaching them to use C++ reasoning. Machine reasoning works until it doesn't work. If you bother to learn the rules of C++ you would understand why this breaks (use of invalidated pointer to automatic storage variable after scope) and you might be able to better reason about the performance of code too. There is nothing wrong with using constexpr variables with automatic storage, the problem you demonstrate is completely unrelated to that.
@cppweekly
@cppweekly Год назад
It seems you chose one small bit of terminology to focus on and completely missed the point of the video. The point is that if you do not use static, you are explicitly asking the compiler to create a local copy for you in your current scope. Yes, it is an `auto`matic variable (let's go ahead and go back to the original C keyword for this discussion). And yes, the compiler *might* choose to optimize away that data copy. In many cases the compiler does not make this optimization. So you have missed the following points: 1) I teach C++. People regularly conflate `constexpr` with "object lifetime" (they are not related to each other in any way). Which is why I made this video, to dispel that myth 2) Local (automatic) copies of large blocks of constexpr computed data can be a significant performance bottleneck if you are not careful 3) I have published several puzzle books with the explicit goal of helping C++ programmers understand the different types of object lifetime and their implications in code: leanpub.com/u/jason_turner
@ryannicholl8661
@ryannicholl8661 Год назад
But you keep suggesting to think of this in terms of stack/heap when such concepts do not exist in C++ instead of using C++ terminology. Pointing out that "constexpr" is not a storage duration is enough to solve the problem. The same issue would happen if you did not use constexpr. What that means is, constexpr is unrelated to the problem. Instead of "use static constexpr" which is bad advice, you could have just pointed out the correct usage of automatic storage variables is not influenced by constexpr. An example, I see a lot of people in my industry think they can do things like use "volatile bool" to synchronize different threads, (which you cannot), which is a direct result of people being taught to think of things in terms of machine or assembly language concepts. There are subtle differences between C++ concepts and the machine concepts. As an example where C++ and machine logic differ, a volatile write in C++ compiles (in isolation) to a store instruction on x86, which has release semantics. However, from the C++ perspective, the release does not exist which allows compiler optimizations that can break things if you think of C++ as equivalent to the generated machine code. Some people even have the audacity to call it a "false positive" in ThreadSanitizer because they simply do not understand the rules of C++. It is not entirely the same, but it's just another example of the harm that results from people using machine/assembly logic instead of C++ logic. The subtle differences between the rules of C++ and machine code are what break things and we all need to teach people to avoid the train of thinking of C++ as an abstraction of machine code (because it is not). We can help people by using C++ terminology and not machine code terminology so they maintain the right frame of reference and maybe actually go read up and understand how things work. But instead all you are doing here is providing a mythical guideline as a substitute for teaching the underlying concept (C++ storage durations and particularly how they interact with constexpr). So yes, this terminology does matter, because now people will go Google "stack" instead of "C++ storage duration" and not be exposed to the formalism of the language.
@alienpioneer
@alienpioneer 2 года назад
Thanks ! Unsubscribed.
@alanwest6949
@alanwest6949 7 месяцев назад
I see this problem you experienced would happen without constexpr. Is this video due to the unresolved emotional issues, in taking 5 years to fix a constexpr problem? I see that if you heal the unresolved emotional issues, you’ll see things clearer and be able to take responsibility.
@Revin2402
@Revin2402 Год назад
Wow this video is very bad, sry.
@yyyy-uv3po
@yyyy-uv3po Год назад
I hate those stupid titles. I knew the video would be good, otherwise I'd have ignored it with disdain.
@cppweekly
@cppweekly Год назад
Sorry, RU-vid makes me play the game sometimes.
@ABaumstumpf
@ABaumstumpf Год назад
This shows that the C++ language is getting more unintuitive keywords/extension, and way worse - the diagnostics are now intentionally broken. What is up with all the "no diagnostic needed"? The standard is literally telling us that the compiler KNOWS that the code is not correct but to not tell the developer!?!? And when you do get an error-message during compilation it is either very cryptic, extremely long and cryptic (looking at you templates), or outright wrong like in this case at 10:46.
@SisypheanRoller
@SisypheanRoller Год назад
NDR is not to confuse the developer but because in general it would be impossible or prohibitively expensive to diagnose certain issues.
@zdspider6778
@zdspider6778 7 месяцев назад
5:13 Why are you calling Clang "LLVM"? It's an acronym for "low level virtual machine", but then apparently some "Project LLVM" came up during the 2000's which apparently is a toolkit for building compilers. A collection of modular and reusable compiler and toolchain technologies. 😐 Fucking hell...
@firstname4337
@firstname4337 2 года назад
just don't use C++ its a horrid language use C instead
Далее
C++ Weekly - Ep 400 - C++ is 40... Is C++ DYING?
20:51
Smart Pointers in C++ (Stop Using new?)
17:18
Просмотров 10 тыс.
C++ Weekly - Ep 305 - Stop Using `using namespace`
14:29
C++ Weekly - Ep 315 - constexpr vs static constexpr
19:47
C++ From Scratch: Constexpr
9:45
Просмотров 8 тыс.
🛑 STOP! SAMSUNG НЕ ПОКУПАТЬ!
1:00
Просмотров 85 тыс.