On a ersonnal standpoint, I already was using CRTP (without knowing it) to create singleton. I have two singleton classes: A scoped one, and a global one the global one needs to inherit from your class (through a using myclass = singleton;) the local one is reversed, and you actually need to inherit from it. less secure than, the first one, but gets the job done. I also use it for my test framework, which allows you to inherit from the test class, which automatically subscribes you to the test manager upon declaration (not upon construction, thx static variables). your usage of friend here saved my headaches of finding a way to prevent the user to subscribe two times the same class by error In this video however, I fail to see the crtp usage (most of the implementations shown are normal classes which inherit from normal classes).
CRTP is a powerful tool. In this case it's used to extend classes. You are right, the classes that are being extended are normal classes. The extension itself is where CRTP is being used.
I did not understant the benefits of using CRTP here. The type of a derived class is not even used anywhere in the base classes. It seems to me that we could cut off the whole crtp stuff here without losing anything.
Yes and no. Have a look at the resulting assembly of both cases. It is basically a tradeoff between program size and runtime. With CRTP you will get a unique instantiation for every time you use the baseclass. The instantiation will take up memory and basically belong to that single class only. So all the calls can be inlined and it will run much faster. Without CRTP (just simple inheritance) you will (probably) only have a single baseclass in memory. Every time this class is called you will be spending for the runtime overhead of the function call and the virtual dispatch. You can think of CRTP here as a way to reuse the code without having to write or copy paste it in each class. The effect is the same as if the code would be directly in the extended class.
This doesn’t look like extending classes using CRTP.This is just plain old public inheritance where derived class has access to public base class methods. OR am I missing something here?
True. It mostly is. The key difference is, that the extension is done at compile time without any runtime overhead. Normal inheritance usually has a runtime overhead, as all the calls need to pass via the v-table.
I'm also confused by this. Origin2d doesn't have any virtual functions so why would there be a vtable with runtime overhead? The only difference I see to normal inheritence is the access to the private member in the base class by using friend. I really enjoy your channel btw!
@@johanyngman7793 Yes, I agree too. What this does is to create a new base class implementation at compile time each time a new class is derived from the template. And the friend declaration actually can be replaced by simply declaring the members protected instead of private. While one doesn't have to pay the price of vtable calls (hmm... what about the virtual dtor?), one does pay the price of code bloat instead, potentially affecting the runtime (e.g., more paging or cache invalidation).
Your videos are realy some nice high quality stuff, keep on keeping on!!
Thanks, will do!
On a ersonnal standpoint, I already was using CRTP (without knowing it) to create singleton.
I have two singleton classes:
A scoped one, and a global one
the global one needs to inherit from your class (through a using myclass = singleton;)
the local one is reversed, and you actually need to inherit from it. less secure than, the first one, but gets the job done.
I also use it for my test framework, which allows you to inherit from the test class, which automatically subscribes you to the test manager upon declaration (not upon construction, thx static variables). your usage of friend here saved my headaches of finding a way to prevent the user to subscribe two times the same class by error
In this video however, I fail to see the crtp usage (most of the implementations shown are normal classes which inherit from normal classes).
CRTP is a powerful tool. In this case it's used to extend classes. You are right, the classes that are being extended are normal classes. The extension itself is where CRTP is being used.
I did not understant the benefits of using CRTP here. The type of a derived class is not even used anywhere in the base classes. It seems to me that we could cut off the whole crtp stuff here without losing anything.
Yes and no. Have a look at the resulting assembly of both cases. It is basically a tradeoff between program size and runtime.
With CRTP you will get a unique instantiation for every time you use the baseclass. The instantiation will take up memory and basically belong to that single class only. So all the calls can be inlined and it will run much faster.
Without CRTP (just simple inheritance) you will (probably) only have a single baseclass in memory. Every time this class is called you will be spending for the runtime overhead of the function call and the virtual dispatch.
You can think of CRTP here as a way to reuse the code without having to write or copy paste it in each class. The effect is the same as if the code would be directly in the extended class.
can you do explicit specialization with CRTP,? I mean, put the definition and implementation in separate files (.hpp and .cpp)
Yes, in case you know all of the instances.
This doesn’t look like extending classes using CRTP.This is just plain old public inheritance where derived class has access to public base class methods.
OR am I missing something here?
True. It mostly is. The key difference is, that the extension is done at compile time without any runtime overhead. Normal inheritance usually has a runtime overhead, as all the calls need to pass via the v-table.
I'm also confused by this. Origin2d doesn't have any virtual functions so why would there be a vtable with runtime overhead? The only difference I see to normal inheritence is the access to the private member in the base class by using friend.
I really enjoy your channel btw!
@@johanyngman7793 Yes, I agree too. What this does is to create a new base class implementation at compile time each time a new class is derived from the template. And the friend declaration actually can be replaced by simply declaring the members protected instead of private. While one doesn't have to pay the price of vtable calls (hmm... what about the virtual dtor?), one does pay the price of code bloat instead, potentially affecting the runtime (e.g., more paging or cache invalidation).