7

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

relaxxx
  • 7,566
  • 8
  • 37
  • 64
  • 1
    Does this warning get triggered in both Debug and Release builds? What if the destructor actually does something? – Banex Aug 19 '15 at 09:43
  • @Banex If destructor does something, everything is fine. Once destructor (even empty one) is defined, everything is fine. – relaxxx Aug 19 '15 at 09:46
  • @relaxxx Well then calling an empty destructor is a no-op and it is like not calling it altogether, hence the warning. It should trigger also when you define an empty one but it is probably a compiler quirk. – Banex Aug 19 '15 at 09:50

1 Answers1

7

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).

DevSolar
  • 67,862
  • 21
  • 134
  • 209