gpt4 book ai didi

c++ - 为什么函数模板的显式实例化不能使用 inline 或 constexpr

转载 作者:行者123 更新时间:2023-11-30 03:34:27 26 4
gpt4 key购买 nike

引用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

这些主题,inlineconstexpr,看起来是分开的,不相关的。为什么存在这个限制?

最佳答案

因为它们的用途相反。

显式实例化的要点是,在 文件中,为项目所需的模板提供定义,这样您就不必在header file 中完全定义模板。 .

inline 的要点是允许在header 中定义函数 - 这样跨多个翻译单元的函数的多个定义可以合并为一个。

constexpr 函数必须具有对编译器可见的定义,以便能够在编译时实际调用它们。没有链接时 constexpr

显式实例化 inlineconstexpr 函数没有意义 - 这些函数模板必须已经在头文件中定义,因此可以隐式实例化按需实例化。

关于c++ - 为什么函数模板的显式实例化不能使用 inline 或 constexpr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com