C ++内联函数:是这样声明,是这样定义还是两者都定义?为什么? [英] C++ inline functions: declare as such, define as such, or both? Why?

查看:233
本文介绍了C ++内联函数:是这样声明,是这样定义还是两者都定义?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使foo是内联定义的,但没有这样声明,并且bar是内联声明的,但没有这样定义,下面的代码段编译也没有问题.

The following code segment compiles with no problems, even though foo is defined inline but not declared as such, and bar is declared inline but not defined as such.

int foo();
inline int foo() { return 3; }

inline int bar();
int bar() { return 4; }

inline int foobar();
inline int foobar() { return 5; }

int main(){
    // ...
}

我的第一个问题:编译器是否以内联方式读取foo? bar呢?这是C ++标准指定的吗?

My first question: does the compiler read foo as inline or not? What about bar? Is this specified by the C++ standard?

我的第二个问题:声明和定义内联函数的最佳做法是哪一种?是foo吗? bar?还是foobar?为什么?

My second question: Which one of these is the best practice in declaring and defining inline functions? Is it foo? bar? or foobar? Why?

inb4我还阅读了其他与此相关的文章,但没有一个直接回答我的问题.

inb4 I read some other posts related to this but none of them answer my question directly.

答案似乎表明foo是内联的,但没有提及bar.它还没有解释为什么为什么 foo比其他方法更受青睐. 答案讨论了何时应该使用内联函数.不用担心:我已经决定使用内联函数.我的问题(确切地说是问题2)是我应该这样声明还是定义它,或者两者都定义,为什么其中一个约定比其他约定更好. 这个问题似乎更接近我的担忧,但没人回答.

This answer seems to suggest that foo is inline, but says nothing about bar. It also doesn't explain why foo is preferred over the others. This answer talks about when I should use inline functions. That's not my concern: I've already decided to use inline functions. My question (question 2, to be precise) is whether I should declare it as such, define it as such, or both, and why one of the conventions is better style than the rest. This question seems to be closer to my concern but nobody answered it.

推荐答案

对于成员函数正确,但对于非成员函数未明确定义(我相信)

请参见ISO C ++ std中的§10.1.6.

True for member functions, and not explicitly-defined for non-member functions (I believe)

See §10.1.6 in ISO C++ std.

内联说明符只能应用于声明或 变量或函数的定义

The inline specifier can be applied only to the declaration or definition of a variable or function

带有内联说明符的函数声明(11.3.5、12.2.1、14.3)声明了内联函数.

A function declaration (11.3.5, 12.2.1, 14.3) with an inline specifier declares an inline function.

它没有明确说明如果内联说明符仅修改函数的定义会发生什么情况.

It doesn't explictly state what will happen if an inline specifier only modifies the definition of a function.

我们可以肯定的是,保证这样的 member 函数被标记为内联(感谢James Curran).

What we can be sure of is that such member functions are guaranteed to be marked as inline (thanks to James Curran).

请参阅第12.2.1节.

See §12.2.1.

内联成员函数(静态或非静态)也可能是 在其类定义之外定义的情况提供了 类定义中的声明或其外部的定义 类定义将函数声明为内联或constexpr.

An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline or constexpr.

在GCC和非成员情况下的所有三个功能

与在GCC -O1 C ++模式下一样,所提到的每个函数都是内联的.

All three functions in GCC and non-member circumstance

As in GCC -O1 C++ mode, every function mentioned are inlined.

代码:

#include "stdio.h"

int foo();
inline int foo() {int i; for(i=0;i<100000;i++); return i+3; }

inline int bar();
int bar() {int i; for(i=0;i<100000;i++); return i+4; }

inline int foobar();
inline int foobar() {int i; for(i=0;i<100000;i++); return i+5; }

int foobar2();
int foobar2() {int i; for(i=0;i<100000;i++); return i+6; }

int main(){
    int a,b,c,d;
    a=foo();
    b=bar();
    c=foobar();
    d=foobar2();
    printf("%d %d %d %d", a, b, c, d);
}

反汇编:

我们只能看到foobar2被调用.

-O2-O3中一样,inline没什么大不了的.编译器将自行决定(在上述情况下,所有4个函数均已内联).

As in -O2 and -O3, inline doesn't matter so much. The compiler will decide by itself (in the case above, all 4 functions are inlined).

这篇关于C ++内联函数:是这样声明,是这样定义还是两者都定义?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆