簡體   English   中英

MSVC: static 結構 std::atomic<bool> Test::g_test 有不同的類型</bool>

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

我的 Visual Studio 17 (2022) 項目收到以下警告,我可以將其簡化為以下內容:

測試1.cpp

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

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

測試2.cpp

#include <atomic>

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

#include "test.h"

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

測試.h

#pragma once

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

結果:

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

我是否違反了一些 C++ 規則? 它是 MSVC 錯誤嗎? 什么是最好的修復/解決方法?

這是一個編譯器錯誤。 請參見https://github.com/microsoft/STL/issues/3241https://developercommunity.visualstudio.com/t/10207002

從 STL 錯誤報告https://github.com/microsoft/STL/issues/3241減少,這似乎實際上是一個編譯器問題。

創建文件a.cpp

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

和文件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; }

然后用“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

值得注意的是,如果S1S0的聲明在b.cpp中重新排序,則不會發出警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM