简体   繁体   English

MSVC: static 结构 std::atomic<bool> Test::g_test 有不同的类型</bool>

[英]MSVC: static struct std::atomic<bool> Test::g_test has different type

I had the following warning with my Visual Studio 17 (2022) project, and I could reduce it to the following:我的 Visual Studio 17 (2022) 项目收到以下警告,我可以将其简化为以下内容:

test1.cpp测试1.cpp

#include <atomic>
#include "test.h"

int main() {
    Test::g_test = true;
}

test2.cpp测试2.cpp

#include <atomic>

struct A {
    std::atomic<bool> m_test = false;
};

#include "test.h"

void a() {
    Test::g_test = true;
}

test.h测试.h

#pragma once

struct Test {
    static inline std::atomic<bool> g_test = false;
};

Result:结果:

1>------ Build started: Project: ConsoleApplication1, Configuration: Release x64 ------
1>test1.cpp
1>test2.cpp
1>LINK : warning C4744: 'static struct std::atomic<bool> Test::g_test' has different type in 'c:\consoleapplication1\test2.cpp' and 'c:\consoleapplication1\test1.cpp': '__declspec(align(1)) struct (1 bytes)' and 'struct (1 bytes)'

Am I violating some C++ rules?我是否违反了一些 C++ 规则? Is it an MSVC bug?它是 MSVC 错误吗? What would be the best fix/workaround?什么是最好的修复/解决方法?

It's a compiler bug.这是一个编译器错误。 See https://github.com/microsoft/STL/issues/3241 and https://developercommunity.visualstudio.com/t/10207002 .请参见https://github.com/microsoft/STL/issues/3241https://developercommunity.visualstudio.com/t/10207002

Reduced from STL bug report https://github.com/microsoft/STL/issues/3241 , this seems to actually be a compiler issue.从 STL 错误报告https://github.com/microsoft/STL/issues/3241减少,这似乎实际上是一个编译器问题。

Create file a.cpp :创建文件a.cpp

 template <class T> struct atomic { alignas(sizeof(T)) T x; }; struct S0 { static inline atomic<bool> z; }; int main() { (void) S0::z; }

and file b.cpp :和文件b.cpp

 template <class T> struct atomic { alignas(sizeof(T)) T x; }; struct S1 { atomic<bool> y; }; struct S0 { static inline atomic<bool> z; }; void f() { (void) S0::z; }

then compile with “cl /nologo /std:c++17 /GL a.cpp b.cpp” which emits:然后用“cl /nologo /std:c++17 /GL a.cpp b.cpp”编译,它发出:

 a.cpp b.cpp warning C4744: 'static struct atomic<bool> S0::z' has different type in 'b.cpp' and 'a.cpp': '__declspec(align(1)) struct (1 bytes)' and 'struct (1 bytes)' Generating code Finished generating code

Notably, no warning is emitted if the declarations of S1 and S0 are reordered in b.cpp .值得注意的是,如果S1S0的声明在b.cpp中重新排序,则不会发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM