tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

bug for private XMLElement constructor

Open elli-one opened this issue 3 years ago • 1 comments

Hi,

I have a construct like std::unique_ptr<XMLElement> mRoot;

So here is always a compile error with Visual Studio and GCC. I seems this is not a good idea to have the constructor private.

/usr/include/c++/10/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = tinyxml2::XMLElement]’: /usr/include/c++/10/bits/unique_ptr.h:182:16: required from ‘void std::__uniq_ptr_impl<_Tp, _Dp>::reset(std::__uniq_ptr_impl<_Tp, _Dp>::pointer) [with _Tp = tinyxml2::XMLElement; _Dp = std::default_deletetinyxml2::XMLElement; std::__uniq_ptr_impl<_Tp, _Dp>::pointer = tinyxml2::XMLElement*]’ /usr/include/c++/10/bits/unique_ptr.h:456:12: required from ‘void std::unique_ptr<_Tp, _Dp>::reset(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = tinyxml2::XMLElement; _Dp = std::default_deletetinyxml2::XMLElement; std::unique_ptr<_Tp, _Dp>::pointer = tinyxml2::XMLElement*]’ /usr/include/c++/10/bits/unique_ptr.h:85:2: error: ‘virtual tinyxml2::XMLElement::~XMLElement()’ is private within this context 85 | delete __ptr; | ^~~~~~~~~~~~

regards elli

elli-one avatar Sep 15 '22 07:09 elli-one

This is not a bug. Your unique_ptr would take ownership of the XMLElement, which this pattern prevents. A private destructor ensures that only a friended type can manage its lifetime. In this case, that type is XMLDocument, which owns all of its elements. In other words, this is intentionally done to make sure that the lifetime of a particular XMLElement is always controlled by its enclosing XMLDocument.

sophec avatar Oct 27 '23 17:10 sophec