Re: Constructing Derived in shell of Base <shudder>
On Jul 16, 9:26 pm, Joshua Maurice <joshuamaur...@gmail.com> wrote:
On Jul 16, 10:57 am, "Francesco S. Carta" <entul...@gmail.com> wrote:
Joshua Maurice <joshuamaur...@gmail.com>, on 16/07/2010 10:37:14, wrote=
:
On Jul 16, 10:26 am, "Francesco S. Carta"<entul...@gmail.com> wrot=
e:
Joshua Maurice<joshuamaur...@gmail.com>, on 16/07/2010 10:16:20, wro=
te:
Still, am
I right to think it would be a perverse implementation which would
have complete objects exist in not-contiguous storage of
sizeof(the_type)?
Nobody can question you if you decide to consider such an implementa=
tion
as "perverse", that's perfectly among your prerogatives :-)
I don't understand all such bothering for something that the standar=
d
allows to implementers (in order to give them additional implementat=
ion
freedom) and forbids to programmers (relying on a particular layout
which could be completely different in another implementation, thus
leading to undefined behavior if relied upon).
In other words: it is something completely out of the programmer's
concern by definition.
It's part of my concern when I use placement new. There I control the
storage and I put complete objects into that storage. I want to
understand the exact extent of the allowance to avoid making
portability mistakes. To know that, I have to understand what it's
trying to achieve and what exactly it allows and what exactly it does
not allow.
I still believe it's not our concern. We are assured that with placemen=
t
new, only sizeof(type) contiguous "chars" are needed/used.
Well, let me phrase the question like this. Is the following code
legal?
#include <new>
class foo
{
public:
virtual void bar() {}
};
int main()
{
foo * f = new foo;
foo * f2 = new (f) foo;
f2 -> bar();
}
[snip long question]
The code looks like everything from terrible and useless and harmless
and legal with foo and main implemented like that. The question
remains that ... what it does? What effect it gives? You may probably
even implement foo::bar to "delete this;" (stops leaking foo object
memory) but same question still remains. I see no other purpose but to
participate in the contest of Weekly Top Five (WTF).
Worse. Just imagine foo being something useful, like for example a
"typedef boost::shared_ptr<int> foo;" and main attempting to do
something useful with f and f2. There whole minefield of undefined
behaviors is suddenly there.
You need to come up with question that makes sense from practical
usefulness POW as well. Then you will see that there are rare reasons
to use placement new in plain open. Its usage is usually hidden into
some template that enforces it into well-defined context.