内联变量如何工作? [英] How do inline variables work?

查看:81
本文介绍了内联变量如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2016年Oulu ISO C ++标准会议上,一项名为内联变量被标准委员会投票选为C ++ 17。

At the 2016 Oulu ISO C++ Standards meeting, a proposal called Inline Variables was voted into C++17 by the standards committee.

用内行人的话来说,什么是内联变量,它们如何工作以及做什么?他们有用吗?内联变量应如何声明,定义和使用?

In layman's terms, what are inline variables, how do they work and what are they useful for? How should inline variables be declared, defined and used?

推荐答案

提案的第一句话:


可以将内联说明符应用于变量

The ​inline specifier can be applied to variables as well as to functions.

inline 的保证效果是允许使用外部链接在多个翻译单元中对功能进行相同定义。在实践中,这意味着在标头中定义函数,可以将其包含在多个翻译单元中。该提案将这种可能性扩展到了变量。

The ¹guaranteed effect of inline as applied to a function, is to allow the function to be defined identically, with external linkage, in multiple translation units. For the in-practice that means defining the function in a header, that can be included in multiple translation units. The proposal extends this possibility to variables.

因此,实际上,(现在已接受)的提案允许您使用内联关键字,用于在标头中定义外部链接 const 命名空间范围变量或任何 static 类数据成员文件,以便使用链接器可以在将标头包含在多个翻译单元中时产生多个定义。它只是选择其中的一个。

So, in practical terms the (now accepted) proposal allows you to use the inline keyword to define an external linkage const namespace scope variable, or any static class data member, in a header file, so that the multiple definitions that result when that header is included in multiple translation units are OK with the linker – it just chooses one of them.

直到C ++ 14为止(包括C ++ 14),它的内部机制一直存在,以支持类模板中的 static 变量,但是没有方便的方法来使用该机制。人们不得不诉诸于诸如

Up until and including C++14 the internal machinery for this has been there, in order to support static variables in class templates, but there was no convenient way to use that machinery. One had to resort to tricks like

template< class Dummy >
struct Kath_
{
    static std::string const hi;
};

template< class Dummy >
std::string const Kath_<Dummy>::hi = "Zzzzz...";

using Kath = Kath_<void>;    // Allows you to write `Kath::hi`.

从C ++ 17开始,我相信有人可以只写

From C++17 and onwards I believe one can write just

struct Kath
{
    static std::string const hi;
};

inline std::string const Kath::hi = "Zzzzz...";    // Simpler!

该提案包含措辞


可以在类定义中定义内联静态数据成员,并且可以指定括号或相等的初始化程序。如果使用 constexpr 说明符声明了该成员,则可以在没有初始化程序的命名空间范围内重新声明该成员(不建议使用此用法,请参见D.X)。其他静态数据成员的声明中不得指定括号-或-等于-初始化器。

​An inline static data member can be defined in the class definition and may s‌​pecify a ​brace­-or­-equal­-initializer. If the member is declared with the constexpr specifier, it may be redeclared in namespace scope with no initializer (this usage is deprecated; see‌​ D.X). Declarations of other static data members shall not specify a ​brace­-or­-equal­-in‌​itializer

struct Kath
{
    static inline std::string const hi = "Zzzzz...";    // Simplest!
};

…正如TC在 a中指出的那样

… as noted by T.C in a comment to this answer.

此外, constexpr 指示符暗示内联用于静态数据成员以及功能。

Also, the ​constexpr​ specifier implies  inline for static data members as well as functions.


注意:

¹对于函数 inline 也会对优化产生暗示作用,即编译器应首选直接替换替换此函数的调用。函数的机器码。此提示可以忽略。

Notes:
¹ For a function inline also has a hinting effect about optimization, that the compiler should prefer to replace calls of this function with direct substitution of the function's machine code. This hinting can be ignored.

这篇关于内联变量如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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