Re: class layout and dummy template parameter

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Tom Widmer [VC++ MVP] <tom_usenet@xxxxxxxxxxx> wrote:
Tom Widmer [VC++ MVP] wrote:

Good point, it would indeed. However, I suppose, in principle, the
compiler could examine the whole program and determine whether a
union exists containing both POD<1> and POD<2> and, only if it
doesn't, then make the aliasing assumption.

C99 gets rid of the need for checking the whole program:

5 One special guarantee is made in order to simplify the use of
unions: if a union contains several structures that share a common
initial sequence (see below), and if the union object currently
contains one of these structures, it is permitted to inspect the
common initial part of any of them __anywhere that a declaration of
the complete type of the union is visible__.


That means that the compiler is free to assume no aliasing unless
there is a prior declaration of a suitable union! I'd hope that C++0x
may have its wording updated to clarify the situation, and perhaps to
match C99.

Something like this might be quite challenging for a C++ compiler,
considering that unions involved might be templates. Not only do you
have to check whether a suspicious union is around, but also whether
some specialization of some template with some parameters may produce
such a union (which may be nested in another class, possibly itself a
template).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.


Quantcast