1

This is a follow on from another question (https://stackoverflow.com/questions/10712659/c-class-design-for-monte-carlol-simulation)

I am planning on implementing a statistical distribution class, using templates. I want to make Distribution a property of an Entity class. This Distribution class can take a few different forms - TriangleDistribution, NormalDistribution and WeightedDistribution, but these are only known at runtime. They share most methods, but each type may have some custom methods too eg. setMean for NormalDistribution, setWeights for WeightedDistribution.

As I understand it, C++ templates refer to a type, which is then used to determine which implementation to use. It was suggested that I implement the different distribution types using templates.

While I think I understand the concepts of C++ templates, I'm not sure how I would go about implementing them to solve this Distribution problem. Do I use template specialialization create something like the following?:

template <WeightedDistribution>
class Distribution {
    WeightedDistribtion wd;
  public:
    Distribution () {}
    double sample () {
      // Custom implementation of sample
      // for weighted distribution
    }
};

// class template specialization:
template <>
class Distributionr <NormalDistribution> {
    NormalDistribtion nd;
  public:
    Distribution () {}
    double sample ()
    {
      // Custom implementation of sample for 
      // a normal distribution
    }
};

This would necessitate creating numerous types for each distribution type. TIA guys. Pete

Community
  • 1
  • 1
Pete855217
  • 1,570
  • 5
  • 23
  • 35
  • 1
    If those are known at _runtime_ then templates are not for you, templates work with types known at _compile time_. – K-ballo May 26 '12 at 01:53
  • Can you explain why? When I say 'at runtime' I meant simply creating the templated class in main(). – Pete855217 May 26 '12 at 02:30
  • Then you are using the term _runtime_ wrong. By that definition, wouldn't everything be _at runtime_? – K-ballo May 26 '12 at 02:31
  • As in, runtime definition according to things like: if (x) { X *x=new(X);} – Pete855217 May 26 '12 at 03:30
  • The only definition there is `x` and its not at _runtime_. Anyway, instead of wasting time on technicalities, why don't you add to your question a code sample of how do you actually expect to use those distributions? – K-ballo May 26 '12 at 03:31
  • The original question has the details on what I was planning to do. I'm still wondering what's behind your comment 'templates work with type known at compile time"....but leave it for now, I'm a way off finishing the code. – Pete855217 May 26 '12 at 05:27

2 Answers2

0

Check how Boost does it: http://www.boost.org/doc/libs/1_49_0/libs/math/doc/sf_and_dist/html/math_toolkit/dist/dist_ref/dists.html

If you are lucky enough, you may not need to implement anything after all.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Thanks K-ballo, I was going to use boost distributions, but was hoping to wrap them up in my classes to simplify the object model. – Pete855217 May 26 '12 at 02:14
0

Got a good answer on how to implement this wrapper of boost's normal distribution from Rob Kennedy. See https://stackoverflow.com/questions/10831003/efficient-boost-distribution-usage for answer

Community
  • 1
  • 1
Pete855217
  • 1,570
  • 5
  • 23
  • 35