Clang compiler produces warning compiling this code snippet and I can't figure out why.
const int* Get() {
static const int ARRAY[4] = {1, 2, 3, 4};
return &ARRAY[0];
}
const int& Test() {
const auto& p = Get();
return (*p);
}
warning: returning reference to local temporary object [-Wreturn-stack-address]
return (*p);
GCC shows no warnings on this code.
I can fix the snippet like this: const auto p = Get();
But I want to know if there is some temporary object and the problem lies deeper
That warning is a false positive, since the pointee of p
is not a temporary, despite p
referring to one. There are more scenarios in which that warning is produced spuriously; See e.g. Bug 21218, which uses
char * f() {
typedef char* t;
const t & r = new char[5];
return r;
}
Presumably, if the return type is a reference, Clang looks for const-references (that have been bound to temporaries) in the returned expression, without considering how they're used.
Answer: Clang's warning is incorrect.
Let's step through what happens here:
static const int ARRAY[4] = {1, 2, 3, 4};
constructs a global array of int
sreturn &ARRAY[0];
returns a pointer to the first element of the global arrayconst auto& p = Get()
stores a reference to a pointer to the first element of a global arrayreturn (*p);
creates a reference to the lvalue of the first element of the global array4 is the tricky one. Clang seems to incorrectly think that *p
is a local value, when in fact we know that it is a global.
Crucial to this proof is the fact that *p
returns an lvalue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With