C++ Weekly - Ep 170 - C++17's `inline` Variables

  Рет қаралды 19,520

C++ Weekly With Jason Turner

C++ Weekly With Jason Turner

Күн бұрын

Пікірлер: 34
@0prahTV
@0prahTV 5 жыл бұрын
developer: I just want to define a constant.. C++: please try every possible combination of modifier keywords until it compiles
@DamianReloaded
@DamianReloaded 5 жыл бұрын
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
@TheMrKeksLp
@TheMrKeksLp 3 жыл бұрын
@@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
@JoeSylve
@JoeSylve 5 жыл бұрын
Why not prefer something like this? struct S { constexpr static std::string_view s = "Hello World"; };
@frel314
@frel314 5 жыл бұрын
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.
@MrSandshadow
@MrSandshadow 5 жыл бұрын
You are the best! Keep going! Never leave us in the c++ abyss without guidance !
@danielrhouck
@danielrhouck 4 жыл бұрын
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.
@hackerhaddi
@hackerhaddi 3 жыл бұрын
this is exactly what i was looking for, thank you^^
@BobSmith-sc6dq
@BobSmith-sc6dq 2 жыл бұрын
Lots of great info, also SO MANY QUESTIONS!!! 😂😂
@danielphd5072
@danielphd5072 5 жыл бұрын
Thanks Jason for #cpp #cplusplus weekly
@vertigo6982
@vertigo6982 5 жыл бұрын
**sobs** he didn't use a lambda in his example :*(
@troctschcpp5263
@troctschcpp5263 5 жыл бұрын
loving string_view
@davidledger5941
@davidledger5941 5 жыл бұрын
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_nobody
@von_nobody 5 жыл бұрын
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.
@khatharrmalkavian3306
@khatharrmalkavian3306 2 жыл бұрын
Why in the world doesn't it just put it in its own compilation unit?
@i_am_acai
@i_am_acai 2 жыл бұрын
Each time this function is called, is a new string constructed?
@cppweekly
@cppweekly 2 жыл бұрын
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_Bhat
@Guruprasad_Bhat 5 жыл бұрын
Can anyone suggest me some good open source c++ 11/14 projects to learn modern c++
@davidledger5941
@davidledger5941 5 жыл бұрын
github.com/fffaraz/awesome-cpp Just search C++17 and you'll get results there :)
@srinivasarajupenumetsa7678
@srinivasarajupenumetsa7678 5 жыл бұрын
Can't we use constexpr static string= "hello"?
@frydac
@frydac 5 жыл бұрын
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..)
@asuasuasu
@asuasuasu 5 жыл бұрын
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.
@asuasuasu
@asuasuasu 5 жыл бұрын
With std::string_view, this is definitively possible, though.
@Alexander-5V
@Alexander-5V 5 жыл бұрын
@@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-5V
@Alexander-5V 5 жыл бұрын
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...
@JackPunter2012
@JackPunter2012 5 жыл бұрын
Thanks for the awesome series, I love it and have watched every episode Also: first, had to be done LOL
@brainplot
@brainplot 5 жыл бұрын
That factory function at the end can also be noexcept can't it?
@asuasuasu
@asuasuasu 5 жыл бұрын
The constructor for std::string can throw.
@asuasuasu
@asuasuasu 5 жыл бұрын
... 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-fs6ro
@AlexSmith-fs6ro 5 жыл бұрын
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.
@danielrhouck
@danielrhouck 4 жыл бұрын
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.
C++ Weekly - Ep 181 - Fixing Our bind_front with std::invoke
13:16
C++ Weekly With Jason Turner
Рет қаралды 6 М.
C++ Weekly - Ep 332 - C++ Lambda vs std::function vs Function Pointer
16:30
C++ Weekly With Jason Turner
Рет қаралды 34 М.
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 28 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Smart Sigma Kid #funny #sigma
00:33
CRAZY GREAPA
Рет қаралды 36 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 34 МЛН
C++ Weekly - Ep 125 - The Optimal Way To Return From A Function
13:10
C++ Weekly With Jason Turner
Рет қаралды 78 М.
Global Variables in C++... not as easy as it seems
18:25
The Cherno
Рет қаралды 66 М.
C++ Weekly - Ep 456 - RVO + Trivial Types = Faster Code
10:33
C++ Weekly With Jason Turner
Рет қаралды 13 М.
C++ Weekly - Ep 190 - The Important Parts of C++17 in 10 Minutes
10:53
C++ Weekly With Jason Turner
Рет қаралды 43 М.
C++ Weekly - Ep 457 - I Read C++ Magazines (So you don't have to!)
16:36
C++ Weekly With Jason Turner
Рет қаралды 10 М.
C++ Weekly - Ep 248 - Understand the C++17 PMR Standard Allocators and Track All the Things
21:07
you will never ask about pointers again after watching this video
8:03
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 835 М.
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 28 МЛН