Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2003-04-16 19:14:22


"David Abrahams" <dave_at_[hidden]> wrote in message
news:uk7duo17z.fsf_at_boost-consulting.com...
> Matthias Troyer <troyer_at_[hidden]> writes:
> >
> > I never not needed mutable yet in any code of mine using random number
> > generators. Could you give an example that shows where it is needed?

sure. in this class we generate some random directions for ray tracing

class Random_distribution : public Distribution

{

SHARED_PIMPL;

public:

Random_distribution();

virtual Vec3 direction() const;

virtual Vec3 diffuse_dir() const;

virtual float uni_01() const;

};

behind the scene some random number generator is used and it is called from
const functions.
we don't want to propagate non-constness, since the class is just a source
of random directions ( anyway, such a propagation reminds me of Java's
checked exceptions in the sence that it affects a whole lot of code that is
not supposed to be affected ).

> It's never needed because you can always pass the generator through
> an adaptor:
>
> template <class F, class T = F::result_type>
> struct nullary_const_function
> {
> nullary_const_function(F& f) : f(f) {}
> T operator() const { return f(); }
> F& f;
> };

you mean

     template <class F, class T = typename F::result_type>
    struct nullary_const_function
     {
         nullary_const_function(F& f) : f(f) {}
         T operator()() const { return f(); }
         F& f;
     };

right :-), which, as you say, makes it possible to replace mutable by

struct use_const
{
    void foo() const { generator_.nonconst_function(); ... }
    nullary_const_function<random_number_generator> generator_;
};

As I said in the other mail, I would like to see examples where "misuse"
arises as a direct cause of operator() being const.

best regards

Thorsten


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk