I have a header file foo.h
like this (unrelated stuff omitted):
#pragma once
#include <memory>
class Bar;
struct Foo
{
std::shared_ptr<Bar> getBar();
std::shared_ptr<const Bar> getBar() const
{
return const_cast<Foo*>(this)->getBar();
}
};
The non-const overload of getBar()
is implemented in a .cpp file, which also sees the full definition of Bar
.
When foo.h
is included from another file (which does not see the definition of Bar
), VS 2010 is giving me a warning like this:
warning C4150: deletion of pointer to incomplete type 'Bar'; no destructor called
on the const overload of getBar()
(or actually on something deep in the standard library instantiated from that overload).
My question is whether that warning can safely be ignored.
The way I look at it, there are two member functions of std::shared_ptr<Bar>
being called in getBar() const
: the converting constructor and the destructor.
// converting constructor
template <class Y>
std::shared_ptr<const Bar>::shared_ptr(std::shared_ptr<Y> &&r)
This is used to initialise the return value of getBar() const
from the return value of getBar()
. This does not list any prerequisites (C++11 27.2.2.1 §20-22) which would require Y
(Bar
in my case) to be complete.
// destructor
std::shared_ptr<const Bar>::~shared_ptr()
27.2.2.2 §1 states that when the shared pointer being destroyed is empty, there are no side effects.
I understand why I'm getting the warning - the destructor code also has to care for the situation when delete
has to be called on the stored pointer, and this code would indeed delete an incomplete type. But the way I see it, it can never be reached in my situation, so getBar() const
is safe.
Am I correct, or have I overlooked a call or something which could make getBar() const
actually delete an incomplete type?