Re: Template technicality - What does the standard say?
On Oct 15, 7:03 am, Stephen Horne <sh006d3...@blueyonder.co.uk> wrote:
On Wed, 15 Oct 2008 16:30:54 +1300, Ian Collins
<ian-n...@hotmail.com> wrote:
Stephen Horne wrote:
On Tue, 14 Oct 2008 15:58:15 -0700 (PDT), James Kanze
<james.ka...@gmail.com> wrote:
Sorry, but when virtual inheritance is involved, it's
dynamically defined. At least with all of the compilers I
know, and I don't think it's possible to implement it
otherwise.
I'm sorry, but it's basic textbook. The compiler determines
what is inherited and places each inherited class at a
particualar offset within the derived class, as if it were
member data (which it is).
This isn't true. See 9.2.12 and 10.0.5.
The standard has already been quoted at me, but read James'
point that I was replying too - this isn't about the standard,
it's about all the compilers he knows and what he thinks is
possible.
I challenge you. First, define a class using *ANY* standard
C++ features you please - multiple inheritance and virtual
inheritance included. Lets call this class TEST.
Now, create two instances of TEST. Don't care where you
instantiate so long as you aren't using any further
inheritance - global, local, normal member, static member,
anything.
You've suddenly introduced some new, unrealistic constraints.
Placement new it if you think that'll make a difference. Just
make sure you don't instance any derived classes - both must
be instances of the exact same class, ie TEST.
Now, show me a compiler - *ANY* compiler - that uses different
memory layouts for those two instances.
Basic science. The easiest way to prove that instances of a
single class can be arranged differently in different cases is
to provide an example, and I'm only asking for one.
I gave one in another posting. You've just said above (for the
first time) that you're not interested in that case. So
offsetof can be made to work, except when it can't.
The standard can't be based on such things. If the standard
says it works, then it has to work.
You won't find one. The layout of the class isn't dynamic.
The layout is dynamic. It depends on the context where the
class is used. You can't get around that by saying you don't
want to consider such contexts.
It may not be the same as the layout of any classes that
inherit from it, but that's beside the point. Even multiple
inheritance would break offsets if you're looking at derived
classes.
Not really. Given a class D, which derives virtually from a
class B, the offsets of elements inherited from B depends on how
D is being used---practically, it depends on which constructor
is called (in g++'s implementation), or which hidden argument is
passed to the constructor (in some other implementations I've
used).
Can you imagine what it would be like if one instance of a
class was arranged differently to another instance of that
exact same class?
I can not only imagine it, I've experienced it.
Can you imagine any vaguely coherent reason why a
compiler-writer would choose to introduce pointless
nondeterminism like this?
It's not nondeterminism, but it does depend on the context in
which the class is used. Given the arguments to offsetof, the
compiler does not have enough information to determine that
context.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34