S
Steve Folly
Hi,
I had a problem in my code recently which turned out to be the 'the "static
initialization order fiasco"' problem
(<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>)
The FAQ section describes a solution using methods returning references to
static objects.
But consider:
Maths.h:
namespace Maths
{
const double Pi = 3.14159265358979323846;
const double DegreesToRadians = Pi / 180.0;
const double RadiansToDegrees = 1.0 / DegreesToRadians;
const double RadiansToThousandthsOfMinutes = 180.0 / Pi * 60.0 * 1000.0;
const double FeetToMetres = 0.3048;
const double MetresToFeet = 1.0 / FeetToMetres;
}
Foo.cpp:
namespace Foo
{
const double x = Bar::TwoPi();
}
Bar.h:
namespace Bar
{
double TwoPi()
}
Bar.cpp:
namespace Bar
{
double TwoPi()
{
return Maths.Pi * 2.0;
}
}
(My actual problem was slightly more complex than this, but this is
My problem arose because Maths:i had not been initialised before Foo::x,
Foo::x was equal to zero. Probably lucky to be zero at all, could have been
anything I guess?
The FAQ way to solve this would be to change the constants to functions?
I don't want to change them to macros, but the thought of having to change
these into functions just seems... I dunno... overkill just for the sake of
several constants? (Especially when quite a lot of code uses these
constants, and up until now I think we've been *extremely* lucky!)
Are functions my best way out of this predicament?
The thought occurs that members of numeric_limit<> classes are faced with
the same problem? Is there still the danger here that using numeric_limit<>
static members might not be initialized themselves when used to initialize
other static data?
Any advice appreciated.
I had a problem in my code recently which turned out to be the 'the "static
initialization order fiasco"' problem
(<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>)
The FAQ section describes a solution using methods returning references to
static objects.
But consider:
Maths.h:
namespace Maths
{
const double Pi = 3.14159265358979323846;
const double DegreesToRadians = Pi / 180.0;
const double RadiansToDegrees = 1.0 / DegreesToRadians;
const double RadiansToThousandthsOfMinutes = 180.0 / Pi * 60.0 * 1000.0;
const double FeetToMetres = 0.3048;
const double MetresToFeet = 1.0 / FeetToMetres;
}
Foo.cpp:
namespace Foo
{
const double x = Bar::TwoPi();
}
Bar.h:
namespace Bar
{
double TwoPi()
}
Bar.cpp:
namespace Bar
{
double TwoPi()
{
return Maths.Pi * 2.0;
}
}
(My actual problem was slightly more complex than this, but this is
My problem arose because Maths:i had not been initialised before Foo::x,
Foo::x was equal to zero. Probably lucky to be zero at all, could have been
anything I guess?
The FAQ way to solve this would be to change the constants to functions?
I don't want to change them to macros, but the thought of having to change
these into functions just seems... I dunno... overkill just for the sake of
several constants? (Especially when quite a lot of code uses these
constants, and up until now I think we've been *extremely* lucky!)
Are functions my best way out of this predicament?
The thought occurs that members of numeric_limit<> classes are faced with
the same problem? Is there still the danger here that using numeric_limit<>
static members might not be initialized themselves when used to initialize
other static data?
Any advice appreciated.