Can an inline variable be changed after initialization in C++17?

心已入冬 提交于 2020-01-03 07:19:32

问题


My scenario is the following (it worked in clang but not in gcc)

liba.hpp:

inline int MY_GLOBAL = 0;

libother.cpp: (dll)

#include "myliba.hpp"

void myFunc() {
    //
    MYGLOBAL = 28;
}

someexe.cpp:

RunAppThatUsesBothLibAandLibOther();

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


回答1:


Yes, inline variables can be modified after initialization.

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.



来源:https://stackoverflow.com/questions/59195606/can-an-inline-variable-be-changed-after-initialization-in-c17

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!