On 05/21/12 11:05 AM, mike3 wrote:
<snip>
> The class is supposed to wrap a random number generator and allow you
> to set up a variable that can output random numbers in a preset range
> (set up on construction). The T can be one of various integer types,
> and that's where we run into problems. Namely, I've only ever used
> this with signed types, but I wasn't sure how to make it go with
> unsigned, because the "max check" there has c_RangeMin which is a
> _negative_ (signed!!!!) number. So if you tried a
> "RandomGeneratorRanged<unsigned int> foo(<smth>,<smth>)" _you'd get
> into trouble_ and I realized I better do something about this before
> it bites me in the ass. Is there a superior way to achieve this, that
> will allow for both signed and unsigned use? As making classes for
> each signed and unsigned integer type sounds like a way to make a mess
> with duplicated code. Hence why there's a template there anyway.
Does this help?
#include <iostream>
#include <limits>
template <typename T>
void wibble( T t )
{
if( std::numeric_limits<T>::is_signed )
{
std::cout << "signed" << std::endl;
}
else
{
std::cout << "unsigned" << std::endl;
}
}
int main()
{
wibble(42);
wibble(42U);
}
--
Ian Collins