While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list.
Here is the function that I'm calling in main:
template<class Engine, typename Type, template<typename = Type> class Distribution, class... DistParams>
Type randomGenerator( RE::SeedType seedType, std::size_t seedValue, std::initializer_list<std::size_t> list, DistParams... params ) {
static Type retVal = 0;
static Engine engine = RE::getEngine<Engine>( seedType, seedValue, list );
static Distribution<Type> dist = RD::getDistribution<Type, Distribution>( params... );
retVal = dist( engine );
return retVal;
}
A little bit about the function above:
The Type
represents the return value of the distribution such as int
, unsigned
, char
, etc for Integral Types
that work with distributions such as uniform_int_distribution<IntType>
or Real Types
such as float
, double
for distributions like uniform_real_distribution<RealType>
.
The terms RE
& RD
are typedefs of two classes.
typedef RandomEngine RE;
typedef RandomDistribution RD;
Both classes follow the same pattern as they have delete constructors and all of their methods are declared a static.
The 2nd line in the function is using the template parameter <class Engine>
to represent what kind of engine from the <random>
header file we want to use from the static methods in class RandomEngine{...}
. Each of the engine types has it's own function to create an engine, seed it by the seeding type and seed values and then returns a reference of the engine. All of the functions in the RandomEngine class
are non template functions. So I then went ahead and made generalized function template RE::getEngine<Engine>( parameters );
in this RandomEngine class
that you can see in this function template randomGenerator()
. Then I specialized this function for each engine type. I had no problems with doing that.
This brings me to the next line with the RandomDistribution class
I'm trying to follow a similar pattern as I did with the RandomEngine
I made a generalized function template RD::getDistribution<Type, Distribution>( params... );
Before I get to the RD::getDistribution<...>(...)
function both of the classes above are non templates. The first class RandomEngine
has zero function templates for its engines except the generalized getEngine()
The difference with this class as opposed to the RandomDistribution
is that every function in this class is a function template, because the <random>
library's distribution functions require it. So now I have to not only template this generalized function as I did for getEngine()
I have to also use a variadic parameter pack as different distributions takes a different amount of arguments.
Here is my declaration of my generalized function in the RandomDistribution
class that is found in the header file:
template<typename Type, template<typename = Type> class Distribution, class... DistParams>
static Distribution<Type>& getDistribution( DistParams... params ) {
return getUniformIntDistribution( params... );
}
Then I have this attempt of a specialization for this function in the cpp file for just one of the other distributions:
template<>
static std::uniform_real_distribution<>& RandomDistribution::getDistribution() {
return RandomDistribution::getUniformRealDistribution();
}
I would like to do this for all the other distributions that I'm supporting.
I am using the stand alone function template randomGenerator()
in my main function like this:
{
std::initializer_list<std::size_t> list{};
unsigned val = randomGenerator<std::mt19937, unsigned, std::uniform_int_distribution>
( RE::USE_CHRONO_CLOCK, std::size_t( 12 ), list, 1, 100 );
std::cout << val << std::endl;
}
When I compile RandomGenerator.cpp
file it compiles without error;
however, when I compile main.cpp
I am getting a compiler error stating that it can not convert from std::uniform_int_distribution<int>&
to std::uniform_int_distribution<Type>&
and it is pointing to my class's generalized function template that is declared in RandomGenerator.h
file.
For some reason; Type
is not being assigned or casted to the type that is passed into randomGenerator's
template parameter list.
I'm stuck at this point. I know what the compiler message is saying; I don't what to do to fix. What can be done to resolve this conversion failure?