[英]Why can explicit instantiation of a function template not use inline or constexpr
Referring to cppreference's section on function templates : 参考cppreference的功能模板部分 :
Explicit instantiation of a function template or of a member function of a class template cannot use inline or constexpr
函数模板或类模板的成员函数的显式实例不能使用内联或constexpr
These topics, inline
and constexpr
, seem separate and unrelated. 这些主题,
inline
和constexpr
,似乎是独立且无关的。 Why does this restriction exist? 为什么存在此限制?
Because they serve opposite purposes. 因为它们的目的相反。
The point of explicit instantiation is, in a source file, to provide definitions for templates that your project needs so that you don't have to fully define the template in your header file . 显式实例化的目的是在源文件中为项目所需的模板提供定义,这样您就不必在头文件中完全定义模板。
The point of inline
is to allow function definitions in a header - so that multiple definitions of the function across multiple translation units can be collapsed into one. inline
的要点是允许在标头中使用函数定义-以便可以将跨多个翻译单元的函数的多个定义折叠为一个。
constexpr
functions must have definitions visible for the compiler to actually be able to invoke them at compile-time. constexpr
函数必须具有可见的定义,编译器才能在编译时实际调用它们。 There is no link-time constexpr
. 没有链接时
constexpr
。
It doesn't make sense to explicitly instantiate an inline
or constexpr
function - those function templates must already be defined in header files and so will be able to be implicitly instantiation on-demand. 显式实例化
inline
或constexpr
函数没有意义-这些函数模板必须已在头文件中定义,因此将能够按需隐式实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.