- Expert C++
- Vardan Grigoryan Shunguang Wu
- 317字
- 2021-06-24 16:34:06
Explicit instantiations
A lot of very useful C++ function templates can be written and consumed without ever using explicit instantiation, but we will describe them here just so you know that they do exist if you ever need them. First, let's have a look at the syntax of explicit instantiations before C++11. There are two forms, as shown in the following code:
template return-type
function_name < template_argument_list > ( function_parameter-list ) ;
template return-type
function_name ( function_parameter_list ) ;
An explicit instantiation definition, also known as a directive, forces the instantiation of a function template for certain type(s), regardless of the template function that will be called in the future. The location of the explicit instantiations can be anywhere after the definition of the function template, and it is only allowed to appear once for a given argument list in the source code.
The syntax for explicit instantiation directives, since C++11, is as follows. Here, we can see that the extern keyword is added before the template keyword:
extern template return-type
function_name < template_argument_list > (function_parameter_list );
(since C++11)
extern template return-type
function_name ( function_parameter_list ); (since C++11)
Using the extern keyword prevents implicit instantiations of that function template (see the next section for more details).
Regarding the previously declared app_max() function template, it can be explicitly instantiated using the following code:
template double app_max<double>(double, double);
template int app_max<int>(int, int);
It can also be explicitly instantiated using the following code:
extern template double app_max<double>(double, double);//(since c++11)
extren template int app_max<int>(int, int); //(since c++11)
This can also be done in a template argument deduced way:
template double f(double, double);
template int f(int, int);
Finally, this can also be done like so:
extern template double f(double, double); //(since c++11)
extern template int f(int, int); //(since c++11)
Moreover, there are some other rules for explicit instantiation. If you want to find out more, please refer to the Further reading section [10] for more details.
- AngularJS Testing Cookbook
- TensorFlow Lite移動(dòng)端深度學(xué)習(xí)
- Oracle 11g從入門到精通(第2版) (軟件開發(fā)視頻大講堂)
- Web交互界面設(shè)計(jì)與制作(微課版)
- Unity Virtual Reality Projects
- Mastering OpenCV 4
- RabbitMQ Cookbook
- Java SE實(shí)踐教程
- Spring Security Essentials
- MySQL程序員面試筆試寶典
- C++從入門到精通(第6版)
- HTML5+CSS3+JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- Penetration Testing with the Bash shell
- Java核心編程
- 編程風(fēng)格:程序設(shè)計(jì)與系統(tǒng)構(gòu)建的藝術(shù)(原書第2版)