C++17特性:inline变量

C++有个强大之处是支持header-only(仅头文件)的库。然而直到C++17,只有该库不用到全局变量/对象,才能做到header-only。

从C++17开始,可以在头文件中将变量/对象定义为 inline,如果此定义被多个转译单元使用,它们也全都指向同一个唯一的对象:

  1. class MyClass {

  2. inline static std::string msg{"OK"}; // C++17没问题

  3. ...

  4. };

  5. inline MyClass myGlobalObj; // 即使被多个CPP文件include也没问题

引入 inline变量的动机

在C++中,不允许类/结构体内初始化非 const静态成员变量:

  1. class MyClass {

  2. static std::string msg{"OK"}; // 编译错误

  3. ...

  4. };

在类/结构体外定义变量,如果这个定义是在头文件中,而这头文件被多个cpp文件包含,也是不行的:

  1. class MyClass {

  2. static std::string msg;

  3. ...

  4. };

  5. std::string MyClass::msg{"OK"}; //如果被多个cpp文件包含,则链接错误

根据一次定义规则(one definition rule,ODR),一个(没有 inline)的变量/对象必须在且仅在一个转译单元(一个cpp文件)中定义一次,即使加了包含保护宏也没用:

  1. #ifndef MYHEADER_HPP

  2. #define MYHEADER_HPP

  3. class MyClass {

  4. static std::string msg;

  5. ...

  6. };

  7. std::string MyClass::msg{"OK"}; // 如果被多个cpp文件包含,则链接错误

  8. #endif

问题不在于头文件被包含了多次,而是有2个不同的cpp文件都包含了该头文件导致都定义了 MyClass::msg

基于同样的原因,如果在头文件里定义了你自己的类的实例,也会链接错误:

  1. class MyClass {

  2. ...

  3. };

  4. MyClass myGlobalObject;// 如果被多个cpp文件包含,则链接错误

变通方法

对于有些情况,是有变通方法的:

  • 可以在类/结构体中初始化字面量类型的静态 const成员变量:

  1. class MyClass {

  2. static const bool trace = false; // 可以,是字面量类型

  3. ...

  4. };

然后,这仅限于字面量类型,比如基础的整形,浮点数,或指针类型,如果是类,则仅限于有常量表达式初始化的非静态静态成员变量,不能有用户自定义类型或虚析构函数。

  • 可以定义 inline函数,返回一个 static的局部变量:

  1. inline std::string& getMsg() {

  2. static std::string msg{"OK"};

  3. return msg;

  4. }

  • 可以定义 static成员函数返回该值:

  1. class MyClass {

  2. static std::string& getMsg() {

  3. static std::string msg{"OK"};

  4. return msg;

  5. }

  6. ...

  7. };

  • 可以使用变量模板(C++14引入):

  1. template<typename T = std::string>

  2. T myGlobalMsg{"OK"};

  • 可以为静态成员变量定义类模板:

  1. template<typename = void>

  2. class MyClassStatics

  3. {

  4. static std::string msg;

  5. };

  6. template<typename T>

  7. std::string MyClassStatics<T>::msg{"OK"};

还可以派生:

  1. class MyClass : public MyClassStatics<>

  2. {

  3. ...

  4. };

但是所有这些办法都导致了巨大的开销,更差的可读性以及绕弯的方式使用全局变量。另外,全局变量的初始化可能被推迟到它第一次被使用,而有时我们想在程序启动时就初始化全局对象(比如想用个对象监控整个进程),这就不行了。

使用 inline变量

现在,有了 inline,你只需要在某个头文件中定义一次,就可以用上单一的全局有效的对象,还可以在多个cpp文件中包含:

  1. class MyClass {

  2. inline static std::string msg{"OK"}; // 从 C++17开始就没问题

  3. ...

  4. };

  5. inline MyClass myGlobalObj; // 即使在多个cpp文件中包含也没问题

初始化是在进入到包含该头文件或这些定义的第一个转译单元(一个cpp文件编译成.o)时进行的。

形式上, inline关键字用在这里跟函数声明为 inline有相同的语义:

  • 可以在多个转义单元内定义,所有定义都是同一个。

  • 必须在每个用到的转译单元内定义。
    2个cpp文件包含了相同的头文件,因为有了相同的定义。程序的行为就跟只有一个变量一样。
    你可以试一下把 atomic类型定义到头文件:

  1. inline std::atomic<bool> ready{false};

一般用 std::atomic,必须在定义的时候就初始化。

注意,仍然要保证初始化时类型是完整的。例如,如果一个 struct或 class有一个自定义类型的静态成员变量,则该成员变量只能在该类型声明后被定义为 inline

  1. struct MyType {

  2. int value;

  3. MyType(int i) : value{i} {

  4. }

  5. // one static object to hold the maximum value of this type:

  6. static MyType max; // can only be declared here

  7. ...

  8. };

  9. inline MyType MyType::max{0};

constexpr隐含了 inline

对于静态成员变量,现在 constexpr隐含了 inline,因此从C++17开始,下面的声明也就定义了静态成员变量 n

  1. struct D {

  2. static constexpr int n = 5; // C++11/C++14: 声明

  3. // since C++17: 定义

  4. };

跟下面的代码相同:

  1. struct D {

  2. inline static constexpr int n = 5;

  3. };

在C++17之前,你可能经常会有声明却没有对应的定义。考虑以下声明:

  1. struct D {

  2. static constexpr int n = 5;

  3. };

如果不需要 D::n的定义,这就够了,比如可以把 D::n作为值传递:

  1. std::cout << D::n; //没问题,D::n以值传递给ostream::operator<<(int)

如果 D::n以引用传递给非inline的函数,或是函数调用没被优化掉,这就不合法了。例如:

  1. int twice(const int& i);

  2. std::cout << twice(D::n); //一般会报错

此代码没有优化,遵循一次定义规则(ODR)。当被最优化编译器编译时,它可能会如愿正常工作,也可能会报个链接错误说缺少定义。当被无优化编译时,它几乎肯定会被报错说缺少 D::n的定义。创建一个指针指向静态成员变量则更会因为缺少定义而被报链接错误(但可能有些编译器模式仍然能工作):

  1. const int* p = &D::n; //通常会报错

同理,C++17之前,要在某个转译单元中明确定义 D::n


  1. constexpr int D::n; // C++11/C++14: 定义

  2. // C++17: 重复声明 (已弃用)

现在,按C++17编译,类内的声明被它自己定义,所以前面所有示例不需要以前那种定义也能用。以前那种定义仍然有效,但已被弃用。

inline变量和 thread_local

使用 thread_local可以使得 inline变量为每个线程生成一份变量:


  1. struct ThreadData {

  2. inline static thread_local std::string name; //一个线程一个name

  3. ...

  4. };

  5. inline thread_local std::vector<std::string> cache; //一个线程一个cache

完整的例子如下:
lang/inlinethreadlocal.hpp


  1. #include <string>

  2. #include <iostream>

  3. struct MyData {

  4. inline static std::string gName = "global"; // 程序中唯一

  5. inline static thread_local std::string tName = "tls"; // 每个线程中唯一

  6. std::string lName = "local"; // 每个对象

  7. ...

  8. void print(const std::string& msg) const {

  9. std::cout << msg << '\n';

  10. std::cout << "- gName: " << gName << '\n';

  11. std::cout << "- tName: " << tName << '\n';

  12. std::cout << "- lName: " << lName << '\n';

  13. }

  14. };

  15. inline thread_local MyData myThreadData; // 一个线程一个对象

然后可以在有 main()的转译单元中使用:
lang/inlinethreadlocal1.cpp


  1. #include "inlinethreadlocal.hpp"

  2. #include <thread>

  3. void foo();

  4. int main()

  5. {

  6. myThreadData.print("main() begin:");

  7. myThreadData.gName = "thread1 name";

  8. myThreadData.tName = "thread1 name";

  9. myThreadData.lName = "thread1 name";

  10. myThreadData.print("main() later:");

  11. std::thread t(foo);

  12. t.join();

  13. myThreadData.print("main() end:");

  14. }

还可以在其他转译单元中定义 foo(),并在其他线程中使用,然后使用前面的头文件:
lang/inlinethreadlocal2.cpp`

  1. #include "inlinethreadlocal.hpp"

  2. void foo()

  3. {

  4. myThreadData.print("foo() begin:");

  5. myThreadData.gName = "thread2 name";

  6. myThreadData.tName = "thread2 name";

  7. myThreadData.lName = "thread2 name";

  8. myThreadData.print("foo() end:");

  9. }

程序会有如下输出:

  1. main() begin:

  2. - gName: global

  3. - tName: tls

  4. - lName: local

  5. main() later:

  6. - gName: thread1 name

  7. - tName: thread1 name

  8. - lName: thread1 name

  9. foo() begin:

  10. - gName: thread1 name

  11. - tName: tls

  12. - lName: local

  13. foo() end:

  14. - gName: thread2 name

  15. - tName: thread2 name

  16. - lName: thread2 name

  17. main() end:

  18. - gName: thread2 name

  19. - tName: thread1 name

  20. - lName: thread1 name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值