'Function to explicitly istantiate a given template function with more standard types?
I have the following question. Supposing I have a header file header.hpp with this template function declaration:
template <typename T> extern T func();
And in the header.cpp file I have the function definition:
template <typename T> T func() { \* do something */ };
//Following lines are for explicit istantiation of the template for some types:
template double func <double> (); //1
template int func <int>(); //2
//And others...
Does exists a function / class or something else which allows me to explicitly istantiate a template class definition for more templated types without the needing of writing something like lines 1 or 2 of the previous code everytime?
I am searching for something like, for example:
void function ( template_function list_of_types )
{
//explicit istantiations of the template_function for the types given in the list_of_types container...
}
The signature doesn't have to be as the one above, but also similar, the important thing is that it works as I said before. Thanks.
Solution 1:[1]
You could use concepts (or requires) and create specialized versions of your template.
So if you have
void func(auto t) { ...}
you could define a concept "AllowedNumericalTypes" and write another template like
void func(AllowedNumericalTypes auto t) { /*1 and 2 */ }
The compiler will choose the most matching version of your func methods and then call those.
btw. template<typename T> void func(T t)
is identical to
void func(auto t)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bernhard Leichtle |
