C ++内联函数和重新声明 [英] C++ inline functions and redeclarations

查看:100
本文介绍了C ++内联函数和重新声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起我的英语。

First of all, sorry for my English.

由于GCC完全忽略了内联说明符,因此我很难知道何时某个函子被内联标记为内联。我与否。我要了解的是,当您对同一功能进行了一些重新声明(在同一翻译单元中或在不同的翻译单元中)时,何时或在何种情况下将您的函数标记为内联(无论编译器将做什么)

Since GCC totally ignores inline specifiers, it's a little difficult for me to know when a funcion has been marked inline by me or not. What I'm trying to understand is, when you have some redeclarations of a same function (in a same translation unit or in different ones), when or under which circunstances your function is marked inline or not (irrespective of what will the compiler make with your hint).

例如:

inline void print();

void print();

或:

void print();
inline print();

用不同的内联说明符重新声明函数的含义是什么?一个更复杂的示例:

What is the meaning of redeclaring a function with different inline specifiers? A more complex example:

#include <iostream>

void print();

int main()
{
    print(); // (1)
}

inline void print() { std::cout << "HELLO" << std::endl; }

从C ++的排他性角度来看,而不是从编译器的角度来看,是 print 函数在行(1)?内联一个吗?

From the exclusive point of view of C++ and not from the point of view of a compiler, is the print function an inline one at line (1)?

我不能简洁地提出问题,但是我认为已经收到消息了:)我试图从C ++和程序员的角度(无论如何)来理解何时应该将函数标记为内联,何时不应该标记为内联

I cannot formulate my question conciselly, but I think the message has been received :) I'm trying to understand when a function is supposed to be marked inline and when not from the point of view of C++ and the programmer's (irrespective of what will the compiler make with your function).

推荐答案

内联允许以两个或多个翻译单元定义外部链接功能(基本上相同)。

inline allows an external linkage function to be defined (essentially identically) in two or more translation units.

这意味着在标头中对其进行定义。

which means defining it in a header.

当函数为 inline 时,必须对其进行定义,而 inline 必须为 1) 同样,在使用它的每个翻译单元中。

when a function is inline it must be defined, and inline, and essentially 1)identically, in every translation unit where it's used.

inline 只是累加特斯函数不能取消-内联

inline just accumulates. a function can't become un-inline.

in除了这些考虑因素之外,内联用作优化提示,编译器可以自由地忽略它们。以这种身份,它表明您希望内联对此函数的机器代码调用。通常,编译器可以更好地确定哪个地方是个好主意。

in addition to these considerations inline serves as an optimization hint, that the compiler may freely ignore. in that capacity it states that you would like the machine code calls to this function to be inlined. generally the compiler can do a better job of deciding where that's a good idea or not.

1 C ++ 17 10.1.6 / 5(dcl.inline / 5):内联函数或变量应在使用过的每个翻译单元中定义,并在其中具有完全相同的定义每种情况

1 C++17 10.1.6/5 (dcl.inline/5): "An inline function or variable shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case"

这篇关于C ++内联函数和重新声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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