Re: Constructing Derived in shell of Base <shudder>
On Jul 18, 6:56 am, =D6=F6 Tiib <oot...@hot.ee> wrote:
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, wro=
te:
On Jul 16, 10:26 am, "Francesco S. Carta"<entul...@gmail.com> wr=
ote:
Joshua Maurice<joshuamaur...@gmail.com>, on 16/07/2010 10:16:20, w=
rote:
Still, am
I right to think it would be a perverse implementation which woul=
d
have complete objects exist in not-contiguous storage of
sizeof(the_type)?
Nobody can question you if you decide to consider such an implemen=
tation
as "perverse", that's perfectly among your prerogatives :-)
I don't understand all such bothering for something that the stand=
ard
allows to implementers (in order to give them additional implement=
ation
freedom) and forbids to programmers (relying on a particular layou=
t
which could be completely different in another implementation, thu=
s
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 t=
he
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 do=
es
not allow.
I still believe it's not our concern. We are assured that with placem=
ent
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.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
1. Post compile'able code: avoid ellipses, such as void f() { ... }
2. Post complete code: put in all necessary #includes and
declarations of needed types and functions
3. Post minimal code: just enough to demonstrate the problem; skip
I/O and calls to libraries if possible
[...]
<<<<
I'm trying to understand parts of the standard which are rarely used
in practice. I have already said that I would not write code like the
sample. The sample's purpose is merely to constrain and make clear my
question which is "Can a compliant implementation allocate a complete
object so that it does not take up a single contiguous block of memory
of sizeof(the_type)?" Perhaps I'm a compiler writer, or perhaps I will
be a compiler writer in the future, in which case this question
matters. Perhaps there will be some code in the future which is
affected by this aspect of the standard, in which case my question
matters. The standard writers thought it relevant to put in the
relevant statements, so I'm just trying to understand their meaning.