为什么函数模板的显式实例化不能使用 inline 或 constexpr
Why can explicit instantiation of a function template not use inline or constexpr
参考cppreference's section on function templates:
Explicit instantiation of a function template or of a member function
of a class template cannot use inline or constexpr
这些主题,inline
和 constexpr
,似乎是独立且不相关的。为什么存在此限制?
因为它们的用途相反。
显式实例化的要点是,在 source 文件中,为您的项目需要的模板提供定义,这样您就不必在header file.
inline
的要点是允许在 header 中定义函数 - 这样跨多个翻译单元的函数的多个定义可以合并为一个。
constexpr
函数必须具有编译器可见的定义,才能在 compile-time 实际调用它们。没有link-timeconstexpr
.
显式实例化 inline
或 constexpr
函数没有意义 - 这些函数模板必须已经在头文件中定义,因此能够隐式实例化 on-demand.
参考cppreference's section on function templates:
Explicit instantiation of a function template or of a member function of a class template cannot use inline or constexpr
这些主题,inline
和 constexpr
,似乎是独立且不相关的。为什么存在此限制?
因为它们的用途相反。
显式实例化的要点是,在 source 文件中,为您的项目需要的模板提供定义,这样您就不必在header file.
inline
的要点是允许在 header 中定义函数 - 这样跨多个翻译单元的函数的多个定义可以合并为一个。
constexpr
函数必须具有编译器可见的定义,才能在 compile-time 实际调用它们。没有link-timeconstexpr
.
显式实例化 inline
或 constexpr
函数没有意义 - 这些函数模板必须已经在头文件中定义,因此能够隐式实例化 on-demand.