unnecessary/unused 模板是否已实例化?

Are unnecessary/unused templates instantiated?

我有一个函数void f<int N>()f 仅使用模板参数 1 和 2 调用,例如f<1>()f<2>() 我已经有效地将 f 专门用于这两个参数。 f 永远不会用任何其他参数调用,但我已经用一般 N 定义了 f,只是为了 "clarity"。问题:编译器是否仍会实例化 f<N>() 的这个未使用的定义? 如果是这样,我觉得这会造成代码膨胀,因为我有许多此类函数位于程序中非常关键的性能区域。

(相反,我正在考虑注释 f<N>() 定义并将其替换为空定义,即 void f<N>(){}。)

除非使用模板,否则不会实例化模板。

来自标准:

§ 14.7.1/10

An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class, or a static data member of a class template that does not require instantiation. It is unspecified whether or not an implementation implicitly instantiates a virtual member function of a class template if the virtual member function would not otherwise be instantiated. The use of a template specialization in a default argument shall not cause the template to be implicitly instantiated except that a class template may be instantiated where its complete type is needed to determine the correctness of the default argument. The use of a default argument in a function call causes specializations in the default argument to be implicitly instantiated.