On Tue, 9 Dec 2003 12:04:45 -0500, "Derek" <no**@none.com> wrote:

Am I correct in assuming there is no guaranteed order of

initialization for a and b in the statement

int a=1, b=a+1;

In other words, b=a+1 could be evaluated before a=1, right?

That's right; no guarantee.

When I have constants that depend on other constants spread about, I

try to write functions instead. The Eiffel language has a neat

solution for this, called 'once' functions, which only compute a

result and evaluate arguments once --the first time they are called;

and from there on after always return the same result without

computing anything. I've been thinking for longer than I care to

compute, about a C++ implementation of lazy initialization and once

functions, but can't find an elegant way, to this day.

Might go something like,

template< typename T, (T fun)() >

struct once

{

typedef < typename T, (T fun)() > once_t;

T *presult;

once() : presult(0) {}

T operator()()

{

if( presult ) return *presult;

presult = new T( fun() );

}

};

And repeat for single and multiple, const and non-const arguments for

fun.