I am working with an external library which can hold large data structures. Calls to it return pointers to objects which it manages:
class ExternalLib {
public:
int* GetLargeObject() { return &large_object; };
private:
int large_object = 1;
};
I am trying to write a wrapper for this external library which can hold a const reference to the large data structure I am interested in:
class ExternalLibraryWrapper {
public:
ExternalLibraryWrapper() : large_object_ref(*ext.GetLargeObject()){};
private:
ExternalLib ext;
const uint &large_object_ref;
};
However, I get the compiler warning:
Reference member 'large_object_ref' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object [clang: dangling_member]
From my understanding, ext.GetLargeObject() returns a temporary pointer to the address of (*this).ext.large_object. Shouldn't the compiler know that the constructed object &large_object_ref isn't actually binding to a temporary object?