什么是内联变量,它是什么有用的? [英] What is an inline variable and what is it useful for?

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

问题描述

在2016年Oulu ISO C ++标准会议上,提案名为内置变量被标准委员会投票成C ++ 17。



在外行人的术语中,什么是内联变量,什么是有用的?

解决方案

提案的第一句话:


inline 说明符可以应用于变量以及函数


应用于函数的 inline 的保证效果是允许在多个翻译单元中用外部链接来相同地定义功能。对于在实践中,意味着定义在头部中的功能,可以包括在多个翻译单元。



因此,在实际中,(现在接受的)建议允许你使用 inline 关键字定义外部链接 const 命名空间范围变量或任何 static 类数据成员文件,以便当该标题包含在多个翻译单元中时产生的多个定义可以使用链接器–它只是选择



直到并包括C ++ 14内部机制已经存在,为了支持 static 类模板中的变量,但没有方便的方法来使用该机制。一个人不得不诉诸于

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

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

使用Kath = Kath_< void> ;; //让你写Kath :: hi。

从C ++ 17开始,我相信只要写

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

inline std :: string const Kath :: hi =Zzzzz ...; //更简单!



< / strong>可以在类定义中定义内联静态数据成员,并且可以指定大括号或初始值。如果成员用 constexpr 说明符声明,它可以在没有初始化器的命名空间范围中重新声明(此用法已弃用;参见D.X)。其他静态数据成员的声明不得指定大括号或等号。


…它允许上面进一步简化为

  struct Kath 
{
static inline std :: string const hi =Zzzzz ...; //最简单!
};

…如TC在 a中所述



此外, constexpr 说明符表示 inline 用于静态数据成员以及函数。







注意:

¹对于函数 inline 也有优化的暗示效果,编译器应该更喜欢用直接替换来替换此函数的调用的函数的机器代码。这个提示可以忽略。


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 is an inline variable and what is it useful for? How should it be declared, defined and used?

解决方案

The first sentence of the proposal:

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

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.

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.

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`.

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!

… in a header file.

The proposal includes the wording

​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

… which allows the above to be further simplified to just

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

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

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


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天全站免登陆