Richard Shaw writes:
error: possibly dangling reference to a temporary [-Werror=dangling-
reference]
315 | const auto & parents =
dae.root().selectNodes("//*[@sid]/..");
This is a complaint about a potential problem that might, just might be
true. It can't be determined without inspecting the contents of the
function. The diagnostic does not, and just assumes the worst.
TLDR: if a function call parameter is a const ref, and gets initialized from
a prvalue, and the function returns the same const ref type, gcc will whine
about a potential dangling reference being used, on the theory that the
function might be returning the same const ref that's getting passed into
it, but the temporary prvalue gets destroyed at the end of the statement.
That's literally what this error means.
The discussion in gcc's bug tracker discovered that gcc will emit this
warning solely based on the function's signature and without considering the
contents of the function. It just assumes that this might be a problem here
and helpfully raises its hand.
The quickest solution is just add a pragma to shut it up.
The code in question[2] is:
// InstanceWithExtra and other <instance_*> with "url" attribute
const auto & instances = root().selectNodes(
Find where selectNodes() is defined. Add
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdangling-reference"
before the definition, and
#pragma GCC diagnostic pop
after it.
This all presumes that this is a false positive, which I'll estimate will be
the case more often than not.
There's a 2nd instance of the error but I think I can fix it once the path
forward on the first is addressed.
What are the odds that the 2nd occurence of this is an actual problem?