Re: How to get rid of VC warning C4239?
ZHENG Zhong wrote:
On Jul 6, 3:32 pm, Ron Natalie <r...@spamcop.net> wrote:
The warning is only benign in broken compilers like VC. Your program
is ill-formed (hence NOT C++). You can not bind rvalues to non-const
references.
Further, all the things you are trying to do to stop the underlying
pimpl from being inappropriately modified is lost, so I'm not sure
what you think you are trying to accomplish.
Thank you for the reply. I think the first choice won't work for me,
since it breaks my xml API (i will explain it later).
I'm not sure which 'first choice' you are referring to, but if you
deliberately choose to not write C++ but to rely on a certain vendor's
extension you are pretty much on your own. In that case you can use
#pragma warning( disable: 4239)
to silence the warning about your code being broken C++ and live with it.
The second choice... sorry but i did not understand what you wanted
to tell me...
I think the current impl's copy semantics is already to copy the PIMPL
(what they do is to copy the boost::shared_ptr object, which is the
PIMPL).
You are copying the pointer, not the thing that it points to, which would be
required to make it work properly.
template<class XmlNode>
class basic_xml_node {
typedef basic_xml_node<XmlNode> node_type;
private:
explicit basic_xml_node(XmlNode* pimpl): pimpl_(pimpl) { }
public:
node_type parent() {
XmlNode* px = pimpl_->parent();
return node_type(px);
}
...
private:
XmlNode* pimpl_;
};
Thus, getting the parent node from one node is trivial:
typedef basic_xml_node<LibxmlNode> xml_node; // or, use MsxmlNode
xml_node child = ...;
xml_node parent = child.parent();
However you call it, your basic_xml_node<> class is more or less a smart
pointer or handle. At the moment, it also behaves like a pointer, copying
the pointer doesn't duplicate the object it points to, i.e. no value
semantics.
I want to keep the API trivial to use, and to disallow client code to
operate directly on pointers. But unfortunately, such implementation
requires that my basic_xml_node is "copyable" (in auto_ptr style).
_ One thing first: auto_ptr is not copyable! _
Instead, auto_ptr is a smart pointer that represents exclusive ownership. In
your case, I think you rather want some kind of shared ownership.
If I understand your first posting correctly, the problem you wanted to
solve is that you pass a "const basic_xml_node<T>" around, but people still
manage to modify it by simply making a copy of it. What I would suggest is
to rather pass a "basic_xml_node<T const>" around, and provide an implicit
conversion from "node<T>" to "node<T const>".
BTW: I don't understand why you even mention the fact that you are wrapping
different XML libs, as far as I am concerned, this is a completely
independent feature.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]