std::vector range check

Discussion in 'C++' started by mk, Apr 1, 2005.

  1. mk

    mk Guest

    I will have 2 build types of my program: debug and release. In debug
    build I want std::vector indexing operation (operator[] or at()) to do
    a range-check. In release build I don't want it.

    What is the easiest way to achieve that?

    I currently have couple solutions, each one with drawbacks:

    1. each time I change a build, search-and-replace in whole code
    vector::eek:perator[] with at() or vice-versa (this is not really a
    solution of course)

    2. derive a class from std::vector and override operator[] to do what
    I need based on DEBUG macro.

    This has a drawback of using non-standard class name (e.g. Vector)
    that can potentially confuse future maintainers of the program.
    Additionally, operator[] is not virtual, so overriding it is not
    entirely safe, as the old one can be invoked accidentally under some
    circumstances.

    cheers,
    Marcin Kalicinski
     
    mk, Apr 1, 2005
    #1
    1. Advertising

  2. mk

    John Carson Guest

    "mk" <> wrote in message
    news:
    > I will have 2 build types of my program: debug and release. In debug
    > build I want std::vector indexing operation (operator[] or at()) to do
    > a range-check. In release build I don't want it.
    >
    > What is the easiest way to achieve that?
    >
    > I currently have couple solutions, each one with drawbacks:
    >
    > 1. each time I change a build, search-and-replace in whole code
    > vector::eek:perator[] with at() or vice-versa (this is not really a
    > solution of course)
    >
    > 2. derive a class from std::vector and override operator[] to do what
    > I need based on DEBUG macro.
    >
    > This has a drawback of using non-standard class name (e.g. Vector)
    > that can potentially confuse future maintainers of the program.
    > Additionally, operator[] is not virtual, so overriding it is not
    > entirely safe, as the old one can be invoked accidentally under some
    > circumstances.
    >
    > cheers,
    > Marcin Kalicinski



    How about:

    #ifdef _DEBUG
    #define at(x) at(x)
    #else
    #define at(x) operator[](x)
    #endif


    --
    John Carson
     
    John Carson, Apr 1, 2005
    #2
    1. Advertising

  3. "mk" <> wrote in message
    news:...
    :I will have 2 build types of my program: debug and release. In debug
    : build I want std::vector indexing operation (operator[] or at()) to do
    : a range-check. In release build I don't want it.
    :
    : What is the easiest way to achieve that?
    Use a non-member function that does what you want.
    Something like [just typing on-the-fly]:
    template<class C>
    C::reference at(C& container, C::size_type index)
    {
    #ifdef NDEBUG
    return container[index];
    #else
    return container.at(index);
    #endif
    }
    // and a const version as well...

    : I currently have couple solutions, each one with drawbacks:
    :
    : 1. each time I change a build, search-and-replace in whole code
    : vector::eek:perator[] with at() or vice-versa (this is not really a
    : solution of course)
    Not a reasonable option.

    : 2. derive a class from std::vector and override operator[] to do what
    : I need based on DEBUG macro.
    Not a good idea either. Only create a new class when you have
    new data members or new invariants to enforce.



    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, Apr 1, 2005
    #3
  4. mk

    Stephen Howe Guest

    >I will have 2 build types of my program: debug and release. In debug
    > build I want std::vector indexing operation (operator[] or at()) to do
    > a range-check. In release build I don't want it.
    >
    > What is the easiest way to achieve that?


    Preprocessor Macros are frowned on as a whole but I use the following for
    vector and deque:

    #if defined(_DEBUG)
    #define AT(x) at(x)
    #else
    #define AT(x) operator[](x)
    #endif

    and then have something like

    std::vector<int> v;
    // later
    int i = v.AT(0);

    You can still use [] if you know you have checked the size() (or .at() if
    not)

    Stephen Howe
     
    Stephen Howe, Apr 1, 2005
    #4
    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. Anonymous
    Replies:
    20
    Views:
    4,305
    Pete Becker
    Mar 30, 2005
  2. Replies:
    46
    Views:
    971
    Antoon Pardon
    Jul 25, 2006
  3. Jason Heyes
    Replies:
    8
    Views:
    728
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,928
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    954
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page