内联静态变量在Visual c ++中的模块之间是否唯一? [英] Are inline static variables unique across modules in visual c++?

查看:85
本文介绍了内联静态变量在Visual c ++中的模块之间是否唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++ 17引入了内联(静态)变量.据说

c++17 introduce inline (static) variables. It is said that

编译器将保证变量只有一个定义并且它只能在所有编译单元中初始化一次."

"The compiler will guarantee that a variable has only one definition and it’s initialised only once through all compilation units."

我想知道可视c ++保证内联静态变量在多个模块(dll和exe)中是否唯一.

I am wondering if visual c++ guarantee inline static variable will be unique across multiple modules (dlls and exe).

//cat.h
class __declspec(dllexport) Cat
{
public:
    inline static int var = 0;
};

如果cat.h包含在多个dll和一个exe中,那么Cat :: var在应用程序中是否唯一?

If cat.h is included in multiple dlls and one exe, is Cat::var unique in the application ?

推荐答案

您的问题是开放式的",但是,如果您实际想要的只是一个实例,则应定义一个宏-说 DLLIMPEXP-有条件地在一个模块(实际上是在其中定义或至少实例化的模块)中定义为 __ declspec(dllexport),并定义为 __ declspec(dllimport)在其他两个.然后有您的标头声明:

Your question is quite 'open-ended' but, if what you actually want is only one instance, then you should define a macro - say DLLIMPEXP - that is conditionally defined as __declspec(dllexport) in one module (where the class is actually defined, or at least instantiated) and as __declspec(dllimport) in the other two. Then have your header declaration:

//cat.h
class DLLIMPEXP Cat
{
public:
    inline static int var = 0;
};

注1:我认为类链接声明将覆盖成员的声明.注意2:不必是要导出的DLL.EXE也可以导出,而DLL也可以导入!注意3:正如其他人所说,C ++ 17标准不适用于(不能)应用于所有链接模块.

Note1: I think the class linkage declaration overrides the member's. Note2: It doesn't have to be a DLL that exports; EXEs can export too, and DLLs can import! Note3: As others have said, the C++17 standard does not (cannot) apply across link modules.

这篇关于内联静态变量在Visual c ++中的模块之间是否唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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