A
aaragon
Hi everyone,
The idea is quite simple: generate a container with random values in
it. For that, I decided to create a class that I called
RandomContainer that inherits from a container (with default value
std::vector<T>). To handle the different ways that the values are
randomized, I created a traits class. The problem that I have, is that
the function randomize() within the traits class takes different
parameters depending on the type (bool takes none, int takes one
parameter and double takes two parameters). Is there a way to make
this function randomize() a generic function? How do I handle the
different parameters? The code is as follows:
// no definition for generic type
template <typename T>
class AlleleTraits {};
// partial specialization for boolean type
template<>
struct AlleleTraits<bool> {
static bool randomize() {
return flip(0.5);
}
};
// partial specialization for integer type
template<>
struct AlleleTraits<int> {
static int randomize(int k) {
return rnd(0, k-1);
}
};
// partial specialization for double type
template<>
struct AlleleTraits<double> {
static double randomize(double low, double high) {
return rnd(low, high);
}
};
// RandomContainer class
template <
typename T,
struct RandomContainer : public Container {
typedef typename Container::iterator iterator;
RandomContainer(size_t size) : Container(size) {}
void initialize(int k) {
for(int i=0; i<this->size(); ++i) {
this->operator[](i) = TTraits::randomize(k);
}
}
void print() {
for(int i=0; i<this->size(); ++i) {
cout<<" "<<this->at(i);
}
}
};
// main.cxx
....
RandomContainer<int> test(10);
test.initialize(6);
test.print();
....
prints:
1 4 3 1 5 1 4 0 3 1
This is fine because the function randomize(int) was defined within
RandomContainer. If I try to do the same with double or bool I have
compiler errors because of course, those functions are not defined
within the traits class.
Thank you,
a²
The idea is quite simple: generate a container with random values in
it. For that, I decided to create a class that I called
RandomContainer that inherits from a container (with default value
std::vector<T>). To handle the different ways that the values are
randomized, I created a traits class. The problem that I have, is that
the function randomize() within the traits class takes different
parameters depending on the type (bool takes none, int takes one
parameter and double takes two parameters). Is there a way to make
this function randomize() a generic function? How do I handle the
different parameters? The code is as follows:
// no definition for generic type
template <typename T>
class AlleleTraits {};
// partial specialization for boolean type
template<>
struct AlleleTraits<bool> {
static bool randomize() {
return flip(0.5);
}
};
// partial specialization for integer type
template<>
struct AlleleTraits<int> {
static int randomize(int k) {
return rnd(0, k-1);
}
};
// partial specialization for double type
template<>
struct AlleleTraits<double> {
static double randomize(double low, double high) {
return rnd(low, high);
}
};
// RandomContainer class
template <
typename T,
struct RandomContainer : public Container {
typedef typename Container::iterator iterator;
RandomContainer(size_t size) : Container(size) {}
void initialize(int k) {
for(int i=0; i<this->size(); ++i) {
this->operator[](i) = TTraits::randomize(k);
}
}
void print() {
for(int i=0; i<this->size(); ++i) {
cout<<" "<<this->at(i);
}
}
};
// main.cxx
....
RandomContainer<int> test(10);
test.initialize(6);
test.print();
....
prints:
1 4 3 1 5 1 4 0 3 1
This is fine because the function randomize(int) was defined within
RandomContainer. If I try to do the same with double or bool I have
compiler errors because of course, those functions are not defined
within the traits class.
Thank you,
a²