How can I fix “& requires l-value” [closed]

蹲街弑〆低调 提交于 2021-01-07 01:42:58

问题


So, I created a project and copied this tutorial in it. When I tried to run it, it gave me this error: C2102 & requires l-value at

m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));

I searched a lot but i found nothing that fits in the context. What can I do to fix it?


回答1:


This is essentially the same build error reported under Issue #652 Error building D3D12MeshShaders (on VS 16.8.0 Preview 3.0) in the DirectX-Graphics-Samples repo.

I'm getting error C2102: '&' requires l-value on many of the lines. Usually it's when a CD3DX12 constructor is directly used with an &, for example [...]

The issue is still open, with an interim workaround given in a comment:

The use of an address of an r-value like this [...] is non-conforming code.

Visual C++ emits a warning C4238 here with /W4 warning level 4, but most VC projects default to level 3 including these samples. [...] Looks like the latest Visual C++ updates for /permissive- have upgraded this to an error.

You can work around this issue for now by disabling /permissive- by changing "Conformance Mode" to "No" in the C/C++ -> Language project settings.




回答2:


The problem is that you are trying to get and than pass the address of an rvalue (specifically a prvalue).

And while that's fine from a lifetime perspective (no reference or pointer to the rvalue escapes the full statement in this case), the language does not know and does not try to find out.

I suggest you add keep() for the reverse to std::move():

template <class T>
constexpr auto& keep(T&& x) noexcept {
    return x;
}

Used like:

m_commandList->ResourceBarrier(1,
    &keep(CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get()),
    D3D12_RESOURCE_STATE_PRESENT,
    D3D12_RESOURCE_STATE_RENDER_TARGET));

Just remember that you are going against the grain, and thus any misuse is your own fault.



来源:https://stackoverflow.com/questions/65315241/how-can-i-fix-requires-l-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!