简体   繁体   English

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

[英]Idiomatic DirectX12 structure initialization breaks on C++20

With MSVC使用 MSVC

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

works fine on C++17 but on C++20 (Preview - Features from the latest working draft) I get the error:在 C++17 上工作正常,但在 C++20(预览 - 最新工作草案中的功能)上我收到错误:

error C2102: '&' requires l-value错误 C2102:“&”需要左值

Does anyone know how it's not causing an error on 17?有谁知道它如何不会在 17 上导致错误? Do we just keep using C++17 for now?我们现在是否继续使用 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 https: //docs.microsoft.com/en-us/windows/win32/direct3d12/helper-structures-for-d3d12 https: //docs.microsoft.com/en-us/windows/win32/direct3d12/ -direct3d-12 中同步资源状态的障碍

This only compiled before because of an MSVC language extension which has now been turned off by default.这只是之前编译的,因为现在默认关闭了 MSVC 语言扩展。 Quoting from Conformance improvements in Visual Studio 2019 version 16.8 - 'Class rvalue used as lvalue' extension :引用Visual Studio 2019 版本 16.8 中的一致性改进 - 'Class rvalue used as lvalue' 扩展

MSVC has an extension that allows using a class rvalue as an lvalue. MSVC 有一个扩展,允许使用 class 右值作为左值。 The extension doesn't extend the lifetime of the class rvalue and can lead to undefined behavior at runtime.该扩展不会延长 class 右值的生命周期,并且可能导致运行时未定义的行为。 We now enforce the standard rule and disallow this extension under /permissive- .我们现在强制执行标准规则并在/permissive-下禁止此扩展。 If you can't use /permissive- yet, you can use /we4238 to explicitly disallow the extension.如果您还不能使用/permissive- ,则可以使用/we4238明确禁止扩展。

This isn't C++20 so much as Visual Studio no longer allowing you to do things that C++ doesn't allow you to do.这不是 C++20,而是 Visual Studio 不再允许您做 C++ 不允许您做的事情。 You cannot get a pointer to an rvalue, and you never could , in any C++ version.在任何 C++ 版本中,您都无法获得指向右值的指针,而且永远也无法获得。 If this was "idiomatic", it was only an idiom for broken compilers.如果这是“惯用语”,那只是损坏编译器的惯用语。

There may be some compiler switch that makes MSVC allow this, but you really shouldn't be using it.可能有一些编译器开关使 MSVC 允许这样做,但你真的不应该使用它。 You're going to have to create a variable, pass a pointer to it, and move on, just like everyone else:你将不得不创建一个变量,传递一个指向它的指针,然后继续,就像其他人一样:

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

https://docs.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance?view=msvc-160 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.从 Visual Studio 2019 版本 16.8 开始,/std:c++latest 选项隐式设置 /permissive- 选项。 It's required for C++20 Modules support.它是 C++20 模块支持所必需的。 Perhaps your code doesn't need modules support but requires other features enabled under /std:c++latest.也许您的代码不需要模块支持,但需要在 /std:c++latest 下启用其他功能。 You can explicitly enable Microsoft extension support by using the /permissive option without the trailing dash.您可以使用 /permissive 选项显式启用 Microsoft 扩展支持,而无需尾随破折号。

Just confirming it will compile on /std:c++latest with the /permissive option只需使用 /permissive 选项确认它将在 /std:c++latest 上编译

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

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