Once my class doesn't have destructor defined, following code gives
warning C4189: 'f' : local variable is initialized but not referenced
(f->~Fred()
isn't recognised as referencing f
)
Is this a bug, or is this standard behaviour?
struct Fred
{
int a, b;
//~Fred(){}
};
int main()
{
char memory[sizeof(Fred)];
void * place = memory;
Fred* f = new(place)Fred();
f->~Fred();
}
This code is of course meaningless (it is minimal working sample), but I get this error in real scenario when writing pool allocator and using it like
template <typename T>
void CallDestructor(T * t)
{
t->~T();
}
I use visual studio 2013 warning level 4 I tested it on some on-line compilers without warning, but I'm not sure what is their warning level
Is this a bug, or is this standard behaviour?
This particular warning ("local variable is initialized but not referenced") is something not required by the standard. As far as the language is concerned, initializing a local variable and then not referencing it is perfectly legal.
It is a sign that your code might not do what you intended it to do, though, so the compiler tries to be helpful and warn you about a questionable construct. ("Have you forgotten something here?") This is completely within the domain of the compiler, so it cannot be "standard behaviour" even if they tried. ;-)
Yes, the compiler should realize that f->...
does constitute a referencing of f
. So the warning is a false posititve. (Probably because the whole thing is optimized away, being a no-op.) Such things happen if you are using high warning levels.
But it's a warning, not an error. You may safely ignore it, or mask it out with #pragma warning
(since you are working with MSVC).
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