|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2007-06-15 12:51:29
shunsuke wrote:
> Eric Niebler wrote:
>> If you are using the above formulation, then the reference is not
>> initialized until runtime. Due to order of initialization issues, you
>> could end up indirecting through an uninitialized reference and crash
>> even before you hit main().
>
> Sorry, could you give me any example such that
> type const &name = get_static_local<type>(); might cause a crash?
//////////
//test.hpp
template<typename T>
T const & get_static_local()
{
static S const s;
return s;
}
struct S { int i; };
namespace
{
S const & s = get_static_local<S>();
}
S const & get1();
S const & get2();
////////////
// test1.cpp
#include "./test.hpp"
S const & get1( )
{
return s;
}
int i = get2().i;
////////////
// test2.cpp
#include "./test.hpp"
S const & get2( )
{
return s;
}
int j = get1().i;
int main()
{
return 0;
}
Compiles and links just fine, crashes and burns even before entering main().
-- Eric Niebler Boost Consulting www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk