在C ++ 17中初始化后可以更改内联变量吗? [英] Can an inline variable be changed after initialization in C++17?
问题描述
我的情况如下(它在clang中起作用,但在gcc中不起作用)
My scenario is the following (it worked in clang but not in gcc)
liba.hpp:
inline int MY_GLOBAL = 0;
libother.cpp:(dll)
libother.cpp: (dll)
#include "myliba.hpp"
void myFunc() {
//
MYGLOBAL = 28;
}
someexe.cpp:
someexe.cpp:
RunAppThatUsesBothLibAandLibOther();
问题是,内联变量在我期望28的地方显示为0,因为它被修改了在运行时。 MSVC对此表示不同意见,但是clang符合我的期望。
The problem is that the inline variable was showing 0 in places where I expected 28 because it was alrady modified at run-time. MSVC disagrees with this, but clang does the thing I would expect.
问题是:在我的方案中,可以在运行时修改内联变量吗?(我通过取消内联变量解决了问题。)
The question is: can inline variables be modified at run-time in my scenario? (I solved the problem by de-inlining the variable.)
推荐答案
是的,内联
变量可以在初始化后进行修改。
Yes, inline
variables can be modified after initialization.
但是,DLL在带有MSVC的Windows上很奇怪。近似地,每个DLL被建模为具有完全独立的运行时的自己的C ++程序。因此,主程序有一个 inline
变量的副本,而DLL是一个副本。
However, DLLs are strange things on Windows with MSVC. To a close approximation, each DLL is modelled as its own C++ program, with an entirely independent runtime. Therefore, there is one copy of your inline
variable for the main program, and another for the DLL.
这篇关于在C ++ 17中初始化后可以更改内联变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!