Re: boost pool per class...
On Nov 17, 6:00 pm, werasm <wer...@gmail.com> wrote:
On Nov 17, 5:43 pm, Marco Manfredini <ok_nospam...@phoyd.net> wrote:
The initialization order withing the TU is not the problem,
but the fact that two or more TU's may use each other during
SI.
I value your contribution, but I still don't see how it can be
a problem in this case. The entire boost::pool library is
implemented in header files (no shared library here). This
implies that all definitions must exist prior to usage in any
translation unit (TU).
All definitions of the templates, yes. We're not talking about
the definition of a template here, however; we're talking about
the definition of an object. Class templates are not objects.
Static data members of class templates are not objects either.
The instantiation of a static data member of a class template is
an object (and that's the issue here). The fact that it's a
template makes the order of initialization problem worse, since
the standard explicitly says that the order is undefined, even
within a single TU (doubtlessly to avoid getting into all of the
problems which would arise if the order depended on the point of
instantiation and the which TU generated the instantiation which
was actually used).
In this case I cannot see how two ore more TU's use each
other, as only one is relevant - that of the derived class.
Perhaps you could strengthen your argument with an example
where this case would fail. I can think of cases where I know
Static initialisation (SI) would be a problem, but not in this
case. Someone, correct me if I'm wrong (In this case SI
cannot be a problem as interdependent TUs are impossible).
#include <cassert>
#include <iostream>
std::ios::Init dummyForInit ;
class S
{
public:
S( char const* p ) ;
char const* getP() const ;
private:
char const* myP ;
} ;
template< typename T >
struct C
{
static S ourS ;
} ;
template< typename T >
S C<T>:: ourS = "whatever" ;
class SS
{
public:
SS() ;
} ;
SS ss ;
int
main()
{
}
S::S(
char const* p )
: myP( p )
{
}
char const*
S::getP() const
{
assert( myP != NULL ) ;
return myP ;
}
SS::SS()
{
std::cout << C<int>::ourS.getP() << std::endl ;
}
With templates, you don't even need separate TU's. (This code
generates an assertion failure with g++ under Linux. And
probably with a lot of other compilers as well.)
--
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