c++ - 类中的内联函数

标签 c++ class inline

<分区>

如果函数体定义在类内部,编译器是否会标记为内联? (即使作者没有标注)

例子:

class F {
public:
    void func() {
    std::cout << "is this inline?\n";
    }
};

最佳答案

是的。

[C++14: 9.3/2]: A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. [..]

但是,除了关联的链接要求之外,这是否具有任何可观察到的效果只能像 inline 关键字一样可以预测。

此规则的原因是通过标题将类定义(成员函数和所有)包含到多个翻译单元中是合法的。否则,您将遇到多重引用 链接器错误。

关于c++ - 类中的内联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463241/

相关文章:

C++ 模板化输出 : iostream or fstream

c++ - 除了 what() 之外,C++ 异常是否应该提供额外的细节?

c++ - 在 C++ 中内联所有方法,没有 Cpp 文件?

c++ - 检查 NaN 数

c++ - 为什么不能使用 constexpr 全局变量来初始化 constexpr 引用类型?

非公共(public)继承中的 C++ 基类方法访问

c++ - 类的静态函数和类的构造函数是什么关系?

python - 控制打印对象时呈现的字符串?

html - 网络邮件客户端截断内联样式

gcc - 如何#define __forceinline 内联?