* Alf P. Steinbach:
* er:
Well, let's flesh this out.
The property you're /stating/ that you're after is something that's done
just once, on first call, and that's the job of a local static variable:
static T log_max_value()
{
struct Check
{
Check( T x ) { assert( !isinf(exp(x)) ); }
};
static T const result = ...;
static Check const theCheck( result ); // Initialized just once.
return result;
}
But this once-on-first-access checking means that the result of
log_max_value is most probably a compile time constant (except for C++
standard's terminology). In that case, the most appropriate is a compile
time assert, not checking at run time. E.g., look up Boosts
BOOST_STATIC_ASSERT.
Huh, I didn't notice that there's an 'exp' function in there, even though I
faithfully copied it from your example. Can't have that at compile time in
C++98. I don't even know it if can be had at compile time even in C++0x.
Which means compile time check is out of the question, at least for C++98.
Sorry about that slip-up (it seems I'm imperfect, who could have guessed?, but
if not for that 'exp' a compile time assertion would have been a good idea).
Cheers, & again, sorry for that about compile time assertion suggestion,
- Alf
PS: It seems that people here expect me to correct my own errors. OK, it happens
that I do. But I think more likely I don't see my own errors and then if nobody
says hey that's wrong then someone reading may keep believing something wrong.