C++20 上惯用的 DirectX12 结构初始化中断

Idiomatic DirectX12 structure initialization breaks on C++20

使用 MSVC

m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(...));

在 C++17 上工作正常但在 C++20 上(预览 - 最新工作草案的功能)我收到错误:

error C2102: '&' requires l-value

有谁知道它为什么不会在 17 上引起错误?我们现在只是继续使用 C++17 吗?

https://docs.microsoft.com/en-us/windows/win32/direct3d12/helper-structures-for-d3d12 https://docs.microsoft.com/en-us/windows/win32/direct3d12/using-resource-barriers-to-synchronize-resource-states-in-direct3d-12

这不是 C++20,因为 Visual Studio 不再允许您做 C++ 不允许您做的事情。您无法获得指向右值的指针,而且在任何 C++ 版本中您永远不可能。如果这是“惯用语”,那它只是用于损坏编译器的惯用语。

可能有一些编译器开关使 MSVC 允许这样做,但你真的不应该使用它。您将必须创建一个变量,将指针传递给它,然后继续,就像其他人一样:

auto temp = CD3DX12_RESOURCE_BARRIER::Transition(...)
m_commandList->ResourceBarrier(1, &temp);

这只是之前编译的,因为 MSVC 语言扩展现在默认关闭。引用自 Conformance improvements in Visual Studio 2019 version 16.8 - 'Class rvalue used as lvalue' extension:

MSVC has an extension that allows using a class rvalue as an lvalue. The extension doesn't extend the lifetime of the class rvalue and can lead to undefined behavior at runtime. We now enforce the standard rule and disallow this extension under /permissive-. If you can't use /permissive- yet, you can use /we4238 to explicitly disallow the extension.

https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-160

Starting in Visual Studio 2019 version 16.8, the /std:c++latest option implicitly sets the /permissive- option. It's required for C++20 Modules support. Perhaps your code doesn't need modules support but requires other features enabled under /std:c++latest. You can explicitly enable Microsoft extension support by using the /permissive option without the trailing dash.

只要确认它会在 /std:c++latest 上使用 /permissive 选项进行编译