I have a header file foo.h like this (unrelated things are 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-constant overload of getBar() implemented in the .cpp file, which also sees the full definition of Bar .
When foo.h included from another file (which does not see the definition of Bar ), VS 2010 gives me a warning similar to this:
warning C4150: deletion of pointer to incomplete type 'Bar'; no destructor called
when overloading const getBar() (or actually on something deep in the standard library created from this overload).
My question is whether this warning can be ignored.
As I look at it, getBar() const calls two member functions std::shared_ptr<Bar> : the conversion 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 initialize the return value of getBar() const from the return value of getBar() . This does not contain any preconditions (C ++ 11 27.2.2.1 Β§20-22), which requires Y ( Bar in my case).
27.2.2.2 Β§ 1 states that when a common pointer is destroyed, there are no side effects.
I understand why I get a warning - the destructor code should also take care of the situation where delete should be called by the stored pointer, and this code will really delete the incomplete type. But, as I see it, it can never be achieved in my situation, so getBar() const is safe.
Am I right, or did I miss a call or something that could make getBar() const actually remove the incomplete type?
c ++ c ++ 11 shared-ptr incomplete-type
Angew
source share