简体   繁体   中英

Idiomatic DirectX12 structure initialization breaks on C++20

With 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:

error C2102: '&' requires l-value

Does anyone know how it's not causing an error on 17? Do we just keep using C++17 for now?

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

This only compiled before because of an MSVC language extension which has now been turned off by default. Quoting from 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.

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. You cannot get a pointer to an rvalue, and you never could , in any C++ version. 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. 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

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.

Just confirming it will compile on /std:c++latest with the /permissive option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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