Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static class functions : self consistency check

Reply
Thread Tools

static class functions : self consistency check

 
 
er
Guest
Posts: n/a
 
      03-20-2009
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-20-2009
* 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

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
 
 
 
er
Guest
Posts: n/a
 
      03-20-2009
On Mar 19, 8:00*pm, er <(E-Mail Removed)> wrote:
>
> * *static void check_bar(){

should be : bool check_bar()

 
Reply With Quote
 
er
Guest
Posts: n/a
 
      03-20-2009
On Mar 19, 8:03*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * 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
>
> --
> Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
> No ads, and there is some C++ stuff! Just going there is good. Linking
> to it is even better! Thanks in advance!


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();
 
Reply With Quote
 
er
Guest
Posts: n/a
 
      03-20-2009

> struct bounds{
> * *static log_max_value(){ static T x = ...; return x;}

Of course I meant
static T log_max_value(){...}
 
Reply With Quote
 
er
Guest
Posts: n/a
 
      03-20-2009

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;
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-20-2009
* 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

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
er
Guest
Posts: n/a
 
      03-20-2009
On Mar 19, 8:26*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * 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
>
> --
> Due to hosting requirements I need visits to <url:http://alfps.izfree.com/>.
> No ads, and there is some C++ stuff! Just going there is good. Linking
> to it is even better! Thanks in advance!


Great, Thanks!
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-20-2009
* 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.


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.

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-20-2009
er wrote:
> On Mar 19, 8:00 pm, er <(E-Mail Removed)> wrote:
>> static void check_bar(){

> should be : bool check_bar()
>


And

bool Foo::is_checked = check_bar();

should be:

bool Foo::is_checked = Foo::check_bar();
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"self.class" vs. "(class << self; self; end)" ssmoot@gmail.com Ruby 6 01-25-2006 03:52 AM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM



Advertisments