Regarding static objects in inline functions existing in multiple dynamic libraries?

海尧 李 0 信誉分
2024-05-11T06:29:03.9233333+00:00

If this code exists in multiple dynamic libraries, can it be guaranteed that the program only has one instance of instance?

//code.h
inline double& fun(){
static double instance = 1.0;
return instance;
}

cppreference Function-local static objects in all definitions of the same inline function (which may be implicitly inline) all refer to the same object defined in one translation unit, as long as the function has external linkage.Is dynamic library linking guaranteed by isocpp?

开发人员技术 | C++
开发人员技术 | C++
一种通用的高级编程语言,作为 C 编程语言的扩展而创建,除了用于低级别内存操作的功能外,还具有面向对象、泛型和功能性等特点。
开发人员技术 | Visual Studio | 其他
开发人员技术 | Visual Studio | 其他
一系列Microsoft集成开发工具套件,用于构建适用于 Windows、Web、移动设备和其他许多平台的应用程序。 不属于特定类别的其他主题。
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. Minxin Yu 13,511 信誉分 Microsoft 外部员工
    2024-05-13T06:29:53+00:00

    您好, @海尧 李

    不行。
    根据文档:Dynamic-Link Library Data
    每个进程都有自己的 DLL 全局变量和静态变量实例。


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。 注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

你的答案

提问者可以将答案标记为“已接受”,版主可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。