What to do about this piece of dangerous code?

Discussion in 'C++' started by mike3, May 21, 2012.

  1. mike3

    mike3 Guest

    Hi.

    I was reworking this program I had done in C++ and I came across this
    little number:

    ---
    template<class T>

    class RandomGeneratorRanged : public RandomGeneratorRangedBase<T> {

    protected:

    T minVal, maxVal;

    public:

    RandomGeneratorRanged() : minVal(0), maxVal(1) {}



    RandomGeneratorRanged(const T minVal_, const T maxVal_)

    : minVal(minVal_), maxVal(maxVal_)

    {

    if(minVal > maxVal)

    std::swap(minVal, maxVal);



    if((static_cast<long>(minVal_) < c_rangeMin) ||

    (static_cast<long>(maxVal_) > c_rangeMax))
    // This is where the problem is
    throw XXXXXException(ERROR_RANDOM_RANGE_OUT_OF_RANGE);

    }


    ....


    };
    ---

    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.
    mike3, May 21, 2012
    #1
    1. Advertising

  2. mike3

    Ian Collins Guest

    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
    Ian Collins, May 21, 2012
    #2
    1. Advertising

  3. mike3

    mike3 Guest

    On May 20, 5:25 pm, Ian Collins <> wrote:
    <snip>
    >    if( std::numeric_limits<T>::is_signed )


    Thanks for the answer!
    mike3, May 21, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Przemo
    Replies:
    5
    Views:
    512
    Teemu Keiski
    Jul 18, 2004
  2. =?Utf-8?B?RGlmZmlkZW50?=

    Question on optimizing a piece of code

    =?Utf-8?B?RGlmZmlkZW50?=, Jun 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    378
    Patrice
    Jun 9, 2005
  3. Hristo Stevic

    highlight the piece of code executed

    Hristo Stevic, Dec 7, 2003, in forum: Java
    Replies:
    0
    Views:
    325
    Hristo Stevic
    Dec 7, 2003
  4. Moritz Beller
    Replies:
    19
    Views:
    666
    Pete Becker
    Jun 4, 2004
  5. Patrick Plattes

    Download a file piece by piece

    Patrick Plattes, Nov 30, 2006, in forum: Ruby
    Replies:
    2
    Views:
    187
    Patrick Plattes
    Nov 30, 2006
Loading...

Share This Page