static class functions : self consistency check

Discussion in 'C++' started by er, Mar 20, 2009.

  1. er

    er Guest

    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.
    er, Mar 20, 2009
    #1
    1. Advertising

  2. * 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!
    Alf P. Steinbach, Mar 20, 2009
    #2
    1. Advertising

  3. er

    er Guest

    On Mar 19, 8:00 pm, er <> wrote:
    >
    >    static void check_bar(){

    should be : bool check_bar()
    er, Mar 20, 2009
    #3
  4. er

    er Guest

    On Mar 19, 8:03 pm, "Alf P. Steinbach" <> 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();
    er, Mar 20, 2009
    #4
  5. er

    er Guest


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

    Of course I meant
    static T log_max_value(){...}
    er, Mar 20, 2009
    #5
  6. er

    er Guest

    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;
    er, Mar 20, 2009
    #6
  7. * 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!
    Alf P. Steinbach, Mar 20, 2009
    #7
  8. er

    er Guest

    On Mar 19, 8:26 pm, "Alf P. Steinbach" <> 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!
    er, Mar 20, 2009
    #8
  9. * 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!
    Alf P. Steinbach, Mar 20, 2009
    #9
  10. er

    red floyd Guest

    er wrote:
    > On Mar 19, 8:00 pm, er <> 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();
    red floyd, Mar 20, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    559
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    578
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    389
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    357
    falcon
    Jul 31, 2005
  5. Replies:
    6
    Views:
    101
    Vivek
    Jan 25, 2006
Loading...

Share This Page