Re: How to write a library with a static object?
John Harrison wrote:
Adrian Hawryluk wrote:
Excuse me, but does anyone know why this fiasco even exist? Why is
there not some dependency mechanism in place? Or something like the
static local object being constructed when the control flows to them
(as opposed to over them)?
It's a good question. One issue would be that with such a scheme the
order of initialisation would be unpredictable, and that wouldn't mesh
well with the one guarantee that the standard does give, which is that
definitions within a single file are initialised in the order that they
occur in that file. I know at times I found that certainty to be useful.
I wasn't saying that order of initialisation as they occur in the source
file should change. I ment that the order of initilisation in the group
of different source files should change based on the dependency of one
source file to another. It seems reasonable and doable. 'make' can do
it based on these dependencies (when given that some compilers can spit
out dependencies based on the included files).
There may be reasons why what you're suggesting isn't easy to achieve, I
don't know, but representatives of the major compiler wiriters work on
the C++ standards board so they wouldn't have decided on this fiasco
with out good reason.
Sounds like they're lazy. :)
One other thing. In that FAQ we have Fred defined as so:
// File x.cpp
#include "Fred.h"
Fred& x()
{
static Fred* ans = new Fred();
return *ans;
}
Why is it not defined like this:
// File x.cpp
#include "Fred.h"
Fred& x()
{
static Fred ans;
return &ans;
}
If you read FAQ 10.14...
Hmm, should have read a little further. :)
> ...he explains why the pointer is used. Personally I
think the issue raised in that FAQ is more of a theoretical concern, I
never been bitten by it in practise (unlike the initalisation order
fiasco).
This just sounds like another dependency issue. Argh, its sooooo doable.
It looks to me that the compilers are built pretty flimsily.
I noticed you didn't say anything about that, but left it in all the
same. :)
Adrian