developer: I just want to define a constant.. C++: please try every possible combination of modifier keywords until it compiles
@DamianReloaded5 жыл бұрын
This is funny but it might also be misleading for people entering the language. Most intricacies in c++ are meant to find the utmost optimal way in which things will compile. If you "just want to define a constant" you have many other simpler ways of doing so. For example: gcc.godbolt.org/z/GWXzDj
@TheMrKeksLp3 жыл бұрын
@@DamianReloaded But why would you "just want to define a const" if there are objectively better ways? In other words: Why make it hard for the programmer to do exactly what he means
@JoeSylve5 жыл бұрын
Why not prefer something like this? struct S { constexpr static std::string_view s = "Hello World"; };
@frel3145 жыл бұрын
I was going to say that it’s specific to string_view which has constexpr constructors hour any type with constexpr constructors) but Jason already used string_view in his example. The static method though can be applied to any class (even the ones without constexpr constructors). And can even avoid complex copy by returning a const reference to a static variable within the function.
@MrSandshadow5 жыл бұрын
You are the best! Keep going! Never leave us in the c++ abyss without guidance !
@danielrhouck4 жыл бұрын
3:20 “And unfortunately, this magic does come with a bit of a cost. We do now have this check for a guard variable here. It does have to do a thread-safe check and initialization of this object-” With you so far 3:33 “-whenever you go to access it *who* is going to initialize it *when* if you have multiple compilation units” Thatʼs not true. It needs to do guarded initialization because it doesnʼt know *who* is going to initialize it, but it does know *when*: at the start of the program before `main`. And that means that it doesnʼt need to do the extra cost on every access, only at program startup, and only because `std::string` has a more complicated constructor (and, I think crucially, a *destructor*). With godbolt.org/z/ac1qcK, neither Clang nor GCC need to do any extra overhead after program start, and neither of them need to do any guarding at all for `S::sv`, even when the variables I test with arenʼt `const` or `constexpr` or inside a function or anything. Sometimes startup time is important, but this is still a one-time cost and not a per-access cost.
@hackerhaddi3 жыл бұрын
this is exactly what i was looking for, thank you^^
@BobSmith-sc6dq2 жыл бұрын
Lots of great info, also SO MANY QUESTIONS!!! 😂😂
@danielphd50725 жыл бұрын
Thanks Jason for #cpp #cplusplus weekly
@vertigo69825 жыл бұрын
**sobs** he didn't use a lambda in his example :*(
@troctschcpp52635 жыл бұрын
loving string_view
@davidledger59415 жыл бұрын
Another way: struct M { static struct { std::string data = "hello, world"; } Value; }; godbolt.org/z/j4825s Not sure its a good idea, but its easier than static when you have a heavy template class.
@von_nobody5 жыл бұрын
Could linker solve this problem? Each TU will emit storage space and init function for this variable and when program will be linked all duplicates will be removed. If nobody access it then it will not be added to final binary. Another think, `constexpr` will probably skip most of initialization code and you will pay only for dummy duplicates.
@khatharrmalkavian33062 жыл бұрын
Why in the world doesn't it just put it in its own compilation unit?
@i_am_acai2 жыл бұрын
Each time this function is called, is a new string constructed?
@cppweekly2 жыл бұрын
If you're referring to the function at the end of the episode - that's returning a `string_view`, not a string. There is no `string` object constructed.
@Guruprasad_Bhat5 жыл бұрын
Can anyone suggest me some good open source c++ 11/14 projects to learn modern c++
@davidledger59415 жыл бұрын
github.com/fffaraz/awesome-cpp Just search C++17 and you'll get results there :)
@srinivasarajupenumetsa76785 жыл бұрын
Can't we use constexpr static string= "hello"?
@frydac5 жыл бұрын
I tend to do this when needing a statc member in a header only class in C++14. Not sure if there are any unwanted implications though. Would have not thought the inline version would be more expensive than the manually out of class definition (not using C++17 yet..)
@asuasuasu5 жыл бұрын
When possible, I think you should use that constexpr version. However, this is not possible here, because std::string does not support constexpr initialization at all. I have heard of dynamic allocation being potentially supported in constexpr contexts for C++20, which could make this possible.
@asuasuasu5 жыл бұрын
With std::string_view, this is definitively possible, though.
@Alexander-5V5 жыл бұрын
@@asuasuasu It could be possible for short strings because many implementations actually use Short String Optimization. When string fits in a short buffer inside a string object it doesn't allocate and in theory can work in the constexpr context. I'm going to try this in my own string class that already implements the most efficient kind of this optimization without need of any extra space. sizeof(String) == 3*sizeof(void*) and it can fit up to 3*sizeof(void*)-1 chars plus terminating null inside without any allocation.
@Alexander-5V5 жыл бұрын
Oh, no! I've just almost rewrote my string class to support constexpr and... found destructor. I forgot that constexpr classes cannot have user-declared destructors... What a stupid limitation? So anyway we have to wait for C++23...
@JackPunter20125 жыл бұрын
Thanks for the awesome series, I love it and have watched every episode Also: first, had to be done LOL
@brainplot5 жыл бұрын
That factory function at the end can also be noexcept can't it?
@asuasuasu5 жыл бұрын
The constructor for std::string can throw.
@asuasuasu5 жыл бұрын
... and as for std::string_view, it depends: en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view IDK why the 4th overload is not noexcept.
@蜜熊胖胖猪 Жыл бұрын
Definitely not a place for junior....
@AlexSmith-fs6ro5 жыл бұрын
The inline solution has the disadvantage that if defined in a header file, a different instance exists in every compilation unit. You cannot use the address as a unique way across the programme, as it will be different.
@danielrhouck4 жыл бұрын
No, the entire point of the `inline` option is that this is *not* the case. The linker merges them all together (you do need to follow ODR, but thatʼs automatic if you use a header file). That is true if you use `static` variables (a different meaning of the keyword than a `static` *class* variable, because the keyword has far too many uses), but not the newer C++17 inline thing heʼs talking about.