Search code examples
c++unique-ptrlvalue

'&' requires l-value on &std::unique_ptr<>.get


I am trying to use the function NuiCreateSensorByIndex(int,INuiSensor**). I am trying not to use naked pointers, so I did std::unique_ptr<INuiSensor> nui; to make it an unique_ptr. Now I want to acces this function, so I do the following: hr = NuiCreateSensorByIndex(i, &nui.get());, but this is wrong:

KinectManager.cpp:29: error: C2102: '&' requires l-value

What am I doing wrong and how to fix it?


Solution

  • The compiler is right: although std::unique_ptr<INuiSensor> can be used to point to things, it is not an object a pointer to which is expected by the NuiCreateSensorByIndex(int,INuiSensor**) function. The reason the function wants a pointer to a pointer is that it wants to modify pointer's content by an assignment of this sort:

    *ptrToPtr = somePtr;
    

    If compiler let you pass a pointer to std::unique_ptr<INuiSensor>, this assignment would be invalid. That's why you need to create a temporary "naked" pointer, pass it to the function, and then assign the result back to std::unique_ptr<INuiSensor>.