S
StephQ
I face two problems related to template classes.
The first one is that you can not have the parameters of a class
automatically deduced by the constructor.
You can solve this problem using make_... template functions (where
now the template arguments get correctly deduced).
However, I still have the following problem, consider:
VariateSampler<
GaussianDistribution,
MersenneTwister>
gaussianSampler( myGenerator, GaussianDistribution() );
Here GaussianDistribution and MersenneTwister are classes, myGenerator
is of type MersenneTwister.
I would really like to write:
VariateSampler<> gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );
VariateSampler<> poissonSampler = make_VariateSampler( myGenerator,
PossionDistribution() );
resulting in gaussianSampler and poissonSampler to be of different
types (deduced from the rhs):
gaussianSampler of type
VariateSampler<MersenneTwisterPseudoRng,GaussianDistribution>
and poissonSampler of type
VariateSampler<MersenneTwisterPseudoRng,PoissonDistribution>
These types are not complicated, but it is only because I simplified
the example, in the real code they are absolutely intractable (no user
could ever write the correct types by hand!).
One solution could be to pass the result of make_... directly where
needed (to a class expecting a template parameter as argument of
constructor), but sometimes I need to pass the same object to two
classes, so this approach is not feasible.
If I use boost::any then I need the exact type of the wrapped object
to cast back to the original type, and I would be back to the original
problem!
And I can not use the type() member function (that returns the type of
the wrapped object) to get back the type of the wrapped object (so
that I correctly always cast to the right stuff) because then I would
be passing a run-time decided type as template parameter.
The main question is: why is not there a C++ keyword like:
static_any gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );
that means that gaussianSampler is the type of RHS (hence avoiding the
user the pain to actually write the extremely complicated type of the
RHS) ?
The compiler is able to check if the type on the RHS is decided on
compile time or not, and give error if it is not.
Is it possible to implement something like this in the current C++
language?
I would really appreciate any help on this point, it is another
recurring problem in my library.
Thank you in advance.
Best Regards
StephQ
The first one is that you can not have the parameters of a class
automatically deduced by the constructor.
You can solve this problem using make_... template functions (where
now the template arguments get correctly deduced).
However, I still have the following problem, consider:
VariateSampler<
GaussianDistribution,
MersenneTwister>
gaussianSampler( myGenerator, GaussianDistribution() );
Here GaussianDistribution and MersenneTwister are classes, myGenerator
is of type MersenneTwister.
I would really like to write:
VariateSampler<> gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );
VariateSampler<> poissonSampler = make_VariateSampler( myGenerator,
PossionDistribution() );
resulting in gaussianSampler and poissonSampler to be of different
types (deduced from the rhs):
gaussianSampler of type
VariateSampler<MersenneTwisterPseudoRng,GaussianDistribution>
and poissonSampler of type
VariateSampler<MersenneTwisterPseudoRng,PoissonDistribution>
These types are not complicated, but it is only because I simplified
the example, in the real code they are absolutely intractable (no user
could ever write the correct types by hand!).
One solution could be to pass the result of make_... directly where
needed (to a class expecting a template parameter as argument of
constructor), but sometimes I need to pass the same object to two
classes, so this approach is not feasible.
If I use boost::any then I need the exact type of the wrapped object
to cast back to the original type, and I would be back to the original
problem!
And I can not use the type() member function (that returns the type of
the wrapped object) to get back the type of the wrapped object (so
that I correctly always cast to the right stuff) because then I would
be passing a run-time decided type as template parameter.
The main question is: why is not there a C++ keyword like:
static_any gaussianSampler = make_VariateSampler( myGenerator,
GaussianDistribution() );
that means that gaussianSampler is the type of RHS (hence avoiding the
user the pain to actually write the extremely complicated type of the
RHS) ?
The compiler is able to check if the type on the RHS is decided on
compile time or not, and give error if it is not.
Is it possible to implement something like this in the current C++
language?
I would really appreciate any help on this point, it is another
recurring problem in my library.
Thank you in advance.
Best Regards
StephQ