c++ explicit instantiation -- function templates -- replace a implicit instantiation to learn it
Im reading c++ primer plus and having some issues understanding how implicit instantiation works. I havent learned classes yet, so I am just dealing with functions. I think I understand the basics of implicit instantiation (function templates) but I jus开发者_如何学运维t dont understand explicit instantiation. I have below a function that uses a template, can someone show me how a function like this would be written if it used a explicit instantiation. You can change the way it works, but just keep it simple. I would really be appreciative. This will help me understand the syntax, and how it is used.
2 #include <iostream>
3
4 template <typename T>
5 void show(T,T);
6
7
8 int main()
9 {
10 int a = 10, b = 12;
11 char c = 'x', d = 'y';
12
13 show(a,b);
14 show(c,d);
15
16 return 0;
17 }
18
19 template <typename T>
20 void show(T a, T b )
21 {
22 std::cout << "I used the int version " << a << " " << b << "\n";
23 }
Just do a Show<int>(c, d)
Notice that I've explicitly instantiated the int
version, but passed the char
parameters. That's (a rather simple take on) explicit instantiation, but you might be actually referring to explicit specialization.
Think of templates as code generation akin to copy/paste where you replace T
by a concrete type. "Instantiation" is the process of copy-pasting the code into your program.
Implicit instantiation happens whenever you use the template. For function templates, the concrete type is deduced from the function arguments and the template is instantiated for that type. So, your code effectively becomes:
void show(int a, int) { std::cout << "I used the int version " << a << " " << b << "\n"; }
void show(char a, char) { std::cout << "I used the int version " << a << " " << b << "\n"; }
int main() { /* as it was */ }
Explicit instantiation, on the other hand, just explicitly forces the compiler to generate code. For example, if we add the explicit instantiation
template void show(double, double);
to your code, the compiler would generate the corresponding code,
void show(double a, double) { std::cout << "I used the int version " << a << " " << b << "\n"; }
This would happen no matter whether or not you actually use that code.
If you don't know about classes yet, then you're missing 92% of the fun, because class template instantiation is far more subtle. Only member functions that are actually used are implicitly instantiated, while an explicit instantiation creates code for all class members. Also, class member functions aren't checked for correctness to some degree until they actually get instantiated, so you can write templates that don't actually make sense for all possible types.
If you meant specialization (which you refer explicit instantiation) for int
then it can be as:
template <typename T>
void show(T,T);
template <> // specialization (not overloading) for 'int'
void show(int a, int b)
{
std::cout << "I used the int version " << a << " " << b << "\n";
}
If you really want to do explicit instantiation then it can be done as:
show<int>(a,b);
which is actually redundant in this case actually.
To explicitly instantiate your function, you want to specify the type explicitly. In your example, replace the calls to show
by:
show<int>(a,b);
show<char>(c,d);
That's it. The implicit instantiation just means the compiler can figure out the int
and char
from the types of the arguments.
精彩评论