Error C2102: '&' requires l-value, VS2019. How to fix?

Viewed 2581

I am using DirectX 11 and trying to load variables locally in scope using XMLoadFloat4. I am doing a lot of it and names are pretty long so I don't want to create bunch of local variables, however with code like this:

XMStoreFloat4(&vertices[0].normal, XMVector3Cross(
        XMLoadFloat4(&(vertices[2].position - vertices[0].position)),
        XMLoadFloat4(&(vertices[0].position - vertices[1].position))
        ));

I'm getting C2102. Do you know elegant way to create reference in place like this?

2 Answers

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

Do you know elegant way to create reference in place like this?

The function expects pointers and you're trying to pass the address of temporaries which isn't allowed. Just store the temporary results in variables and pass the addresses to those:

auto a = vertices[2].position - vertices[0].position;
auto b = vertices[0].position - vertices[1].position;

XMStoreFloat4(
    &vertices[0].normal,
    XMVector3Cross(
        XMLoadFloat4(&a),
        XMLoadFloat4(&b)
    )
);
Related