C++ Templates Template Specialization

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

You can define implementation for specific instantiations of a template class/method.

For example if you have:

template <typename T>
T sqrt(T t) { /* Some generic implementation */ }

You can then write:

template<>
int sqrt<int>(int i) { /* Highly optimized integer implementation */ }

Then a user that writes sqrt(4.0) will get the generic implementation whereas sqrt(4) will get the specialized implementation.



Got any C++ Question?