static class functions : self consistency check

E

er

Hi All,

struct Foo{
static double bar();

static void check_bar(){
//e.g. return bar()>0.0
}

static bool is_checked;
};

bool Foo::is_checked = check_bar();

With the above set up I need to "manually" verify is_checked.

Instead I'd like Foo to perform a "self check" such that assert
(is_checked) is executed.
 
A

Alf P. Steinbach

* er:
Hi All,

struct Foo{
static double bar();

static void check_bar(){
//e.g. return bar()>0.0
}

static bool is_checked;
};

bool Foo::is_checked = check_bar();

With the above set up I need to "manually" verify is_checked.

Instead I'd like Foo to perform a "self check" such that assert
(is_checked) is executed.


Please explain the problem again in different words and perhaps more real code.


Cheers,

- Alf
 
E

er

* er:










Please explain the problem again in different words and perhaps more real code.

Cheers,

- Alf

The original motivation was something like:

template<typename T>
struct bounds{
static log_max_value(){ static T x = ...; return x;}
static void check(){ assert( !isinf(exp(log_max_value())) ); }
};

typedef bounds<double> bounds_t;

I can call bounds_t::check() and then, if no error was generated, do
log_max_value();

but instead I'd like to call
log_max_value(); directly, with an automatic check. The check needs to
be performed only once, the first time, not every time I call
log_max_value();
 
E

er

I guess I can at least do something like

class bounds{
public:
static T log_max_value(){ static bool first_time = true; if
(first_time){check();}else{first_time = false}; return
log_max_value_impl();}
private:
static void check(){ assert( !isinf(exp(log_max_value_impl()))); }
static T log_max_value_impl{ ...}
};

but perhaps someone has thought of something more neat;
 
A

Alf P. Steinbach

* er:
The original motivation was something like:

template<typename T>
struct bounds{
static T log_max_value(){ static T x = ...; return x;}
static void check(){ assert( !isinf(exp(log_max_value())) ); }
};

typedef bounds<double> bounds_t;

I can call bounds_t::check() and then, if no error was generated, do
log_max_value();

but instead I'd like to call
log_max_value(); directly, with an automatic check. The check needs to
be performed only once, the first time, not every time I call
log_max_value();

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.


Cheers & hth.,

- Alf
 
E

er

* 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.

Cheers & hth.,

- Alf

Great, Thanks!
 
A

Alf P. Steinbach

* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top