Boost logo

Boost Users :

Subject: Re: [Boost-users] [Random] Passing an arbitrary uniform random number generator to a function
From: John Dlugosz (JDlugosz_at_[hidden])
Date: 2010-04-28 11:29:12


Your mention of "you need type erasure" makes me think of using boost::function to do the job, which I suppose could replace the explicit writing of the runtime_generator class. But still, nice idea to wrap the virtual function taking the wrapped thing with a non-virtual template member that does the wrapping.

> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of Joseph Gauterin
> Sent: Tuesday, April 27, 2010 6:50 AM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] [Random] Passing an arbitrary uniform random
> number generator to a function
>
> Looks like you need some sort of type erasure (Steve's suggestion uses
> boost::function for that):
>
> * Create a class that can be passed to uniform_real as a generator -
> that is what Steve's runtime_generator class is for.
> * Give runtime_generator a constructor that takes a single argument of
> a template type, initialize min_ and max_ from that and store the
> argument in impl (that will compile if the argument is a rng that
> follows the correct conventions)
> * Write the virtual function you wanted taking an argument of type
> runtime_generator<double>& - but make it private
> * Write a non-virtual public template function to wrap the rng
>
> It's difficult to explain concisely - here's what the code should look
> like (untested - just for explanation):
>
> template<class ResultType>
> class runtime_generator {
> public:
>
> template <class T>
> explicit runtime_generator(T& rng)
> : min_(rng.min()), max_(rng.max()), impl(rng) {
> }
>
> ResultType min() const { return min_; }
> ResultType max() const { return max_; }
> ResultType operator()() { return impl(); }
>
> private:
> ResultType min_, max_;
> boost::function<ResultType()> impl;
> };
>
>
> //in your class...
> public:
> template<class rng_t>
> double foo(rng_t& urng)
> {
> runtime_generator<double> rtRNG(urng);
> return foo_(rtRNG);
> }
>
> private:
> virtual double foo_(runtime_generator<double> &urng)
> {
> double x = uniform_real<double> (0.0, 10.0) (urng);
> double y = uniform_real<double> (5.0, 7.0) (urng);
> return (x*y);
> }
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net