本文介绍了C++模板“延迟实例化"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 模板中的延迟实例化"是什么意思?

What is meant with "deferred instantiation" in C++ templates?

推荐答案

延迟实例化是指模板直到第一次使用相应的实体才实例化.例如,您有一个模板化函数:

Deferred instantiation is when the template is not instantiated until the corresponding entity is used for the first time. For example, you have a templated function:

template<int Size>
void YourFunction()
{
    //does something
}

参数 Size 可以具有 int 可以具有的任何可能值.您是否自动为 Size 的所有可能值实例化了模板化函数?不,模板仅针对代码中首次出现函数调用时实际用作参数的值进行实例化:

parameter Size can have any possible value that int can have. Do you automatically have the templated function instantiated for all possible values of Size? No, the template is only instantiated for the values that are actually used as the parameter when the function call first appears in the code:

YourFunction<100>(); //instantiated for 100

这篇关于C++模板“延迟实例化"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:27